On Sep 16, 12:53 am, suresh <suresh.amritap...@gmail.com> wrote:
> On Sep 15, 1:54 am, James Kanze <james.ka...@gmail.com> wrote:
> > On Sep 13, 9:03 pm, suresh <suresh.amritap...@gmail.com> wrote:
> > > On Sep 10, 9:24 pm, Garrett Hartshaw <gharts...@gmail.com> wrote:
> > > > I'm no expert, but I would say that a smart pointer (e.g.
> > > > std::tr1::shared_pointer) would probably be your best choice.
> > > Garret this means that I can avoid having a destructor if I use the
> > > shared_pointer right?
> > No. It means that you don't have to write one yourself. You
> > still have a destructor; it's just that the compiler generates
> > the correct one automatically.
> or rather as I understood lately, when a shared_ptr is used, instead
> of calling delete in the destructor, one calls reset() which will
> reset the reference count and when it becomes zero, it will be
> automatically deleted. Am I right?
No.
If the compiler generates the destructor, it calls the
destructor of all non-static members. If you write
a destructor, the compiler adds code to it to call the
destructor of all non-static members. The destructor of
shared_ptr does the right thing. (For its defined
semantics---I'm still not convinced that those semantics
correspond to your needs. More likely, you shouldn't be
dynamically allocating anything to begin with.)
--
James Kanze
|