|
Boost Users : |
From: David Abrahams (dave_at_[hidden])
Date: 2003-07-26 20:48:35
Paul Grenyer <pjgrenyer_at_[hidden]> writes:
> On Sat, 2003-07-26 at 20:57, Joshua wrote:
>
> Hi
>
>> typedef std::vector<boost::shared_ptr<Foo> > FooVector;
>>
>> FooVector Foobar;
>> Foo* blah = new Foo();
>> boost::shared_ptr<Foo> * NewPointer = new boost::shared_ptr<Foo>(blah);
>> Foobar.insert(*NewPointer); //Yes I know this looks horrible. And its probably wrong.
>
> The above isn't quite right. You're inserting pointers to shared_ptr's.
> Try this instead:
>
> typedef std::vector<boost::shared_ptr<Foo> > FooVector;
> FooVector Foobar;
> Foobar.push_back( new Foo(blah) );
That won't work either; shared_ptr's constructor is explicit.
you could write
Foobar.push_back(boost::shared_ptr<Foo>(new Foo(blah)))
But Peter Dimov's excellent advice is "manage every newly-allocated
resource immediately with a *named* resource manager object"
(e.g. shared_ptr), leading to:
boost::shared_ptr<Foo> myfoo(new Foo(blah));
Foobar.push_back(myfoo);
-- Dave Abrahams Boost Consulting www.boost-consulting.com
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