|
Boost Users : |
From: Keith Weintraub (Keith_at_[hidden])
Date: 2005-11-23 10:04:08
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 <pandamojo_at_[hidden]>
Subject: Re: [Boost-users] Collections of shared_ptr<Base> and
shared_ptr<Derived>
To: boost-users_at_[hidden]
Message-ID:
<561a17f20511211529l1a528314od7905edcc2aa2ab7_at_[hidden]>
Content-Type: text/plain; charset=ISO-8859-1
On 11/21/05, Keith Weintraub <Keith_at_[hidden]> wrote:
>
>
>
> 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
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net