Re: [Boost-users] Collections of shared_ptr<Base> and shared_ptr<Derived>
Michael,
Thanks for the reply. You are absolutely right.
I'm coming back to C++ after years of Java and I haven't been careful
enough about reading the stl docs.
One quibble with your solution is that I am doing an upcast not a down
cast, but your solution worked perfectly for me.
Thanks all,
KW
________________________________________________
Date: Mon, 21 Nov 2005 15:29:29 -0800
From: Michael Rickert
Folks,
I have come up with the following solution in template form. I would
be ecstatic to receive any feedback on either my solution or original question. I am not an expert in these things and I can use all the help any generous soul could provide.
Here is my solution:
*snip* It would seem you're overengineering things. It'd be simpler just to use std::vector's iterator constructors: std::vector< boost::shared_ptr< derived > > data; ... std::vector< boost::shared_ptr< base > > downcast_data( data.begin() , data.end() ); If you wanted to go with your original version, I'd suggest returning a managed pointer of some sort (std::auto_ptr or boost::shared_ptr) to decrease the likelyhood of a leak - or having the user pass a nonconstant reference to a vector to fill. Sorry if this has allready been covered by the time this is recieved, my posts seem to still be awaiting moderator approval. -Mike
participants (2)
-
Keith Weintraub
-
Michael Rickert