mlimber wrote:
> toton wrote:
> > One more question with smart_ptr ( I am using the one from Axter).
> > I want to assign the object to the pointer later, not at the
> > construction time.
> > like,
> > smart_ptr<Base> pBase;
> > ///what will be the step here to release the NULL pointer and reduce
> > the reference count?
> > pBase = smart_ptr<Base>(new Derived()) ; ///???
> > Here upon deletion it deletes the Derived twice. Thus I want pBase to
> > point nowhere and when only it is assigned, the count to increase.
>
> No. With any smart pointer that is worthy of the name (e.g.,
> std::tr1::shared_ptr, std::auto_ptr, etc.), you don't need to do
> anything. The smart pointer will initialize to null (and the reference
> count, if there is one, will be 0), and since it's safe to delete a
> null pointer
> (http://parashift.com/c++-faq-lite/fr...tml#faq-16.8-), the
> normal reassignment procedure should work fine.
If I write
auto_ptr<Base> pBase;
pBase = auto_ptr<Base>(new Derived(5));
I get the expected result. Derived destructor & Base destructor is
called only once.
However If I use Axter smart_ptr (which is pretty common and has its
origin on Andrei Alexandrescu 's policy based smart pointers ),
smart_ptr<Base> pBase1;
pBase1 = smart_ptr<Base>(new Derived(5));
I get the destructor gets called twice. For a single line statement it
works fine.
can anyone point to the problem. The Base and Derived class are simple,
just to track emmory, has a cout in ctor and dtor.
Thanks
> Cheers! --M