|
Boost Users : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-09-09 08:40:14
From: "Julia Donawald" <yg-boost-users_at_[hidden]>
[...]
> class Vector_Foo
> {
> public:
[...]
> void AddFooObject(Foo* pFoo)
> {
> FooDataPtr ptrFoo(pFoo);
>
> m_vecChildren.push_back(ptrFoo);
> };
As Gennadiy already said, if you want to use shared_ptr to manage memory, do
not use ordinary pointers.
void AddFooObject(shared_ptr<Foo> const & pFoo)
{
m_vecChildren.push_back(pFoo);
}
> protected:
> typedef boost::shared_ptr<Foo> FooDataPtr;
>
> typedef std::vector<FooDataPtr> FooVector;
>
> FooVector m_vecChildren;
> };
>
> int main( )
> {
> Vector_Foo vec;
> Foo* pFoo = new Foo();
If you want to avoid memory-related problems, never use an ordinary pointer
to hold memory allocated with "new".
shared_ptr<Foo> pFoo(new Foo());
> vec.AddFooObject(pFoo);
> vec.AddFooObject(pFoo);
>
> return 0;
> }
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