Connecting Tech Pros Worldwide Help | Site Map

C++ class

stephane
Guest
 
Posts: n/a
#1: Jul 23 '05
Is it possible to have several destructor in a same class?


Ivan Vecerina
Guest
 
Posts: n/a
#2: Jul 23 '05

re: C++ class


"stephane" <stephane.vollet@bluewin.ch> wrote in message
news:420f7d94$1_1@news.bluewin.ch...[color=blue]
> Is it possible to have several destructor in a same class?[/color]
No - because the destructor is implictly called in most
situations, and you wouldn't be able to specify which
destructor to call.

What is done instead is to:
- Call explicitly a member function which will release
resources or do any required specific operations.
- Keep a flag or state variable in the object, which
the destructor checks to know how it should behave.
(this tends to be abused by novices, and is best avoided)


--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com


stephane
Guest
 
Posts: n/a
#3: Jul 23 '05

re: C++ class


So there can be only one destructor.

If I have declared in my class this for instance:

~Tvector(){delete[]m_values;}

there is no ~Tvector(){} anymore?


"Ivan Vecerina" <INVALID_use_webform_instead@vecerina.com> a écrit dans le
message de news: cunv54$591$1@news.hispeed.ch...[color=blue]
> "stephane" <stephane.vollet@bluewin.ch> wrote in message
> news:420f7d94$1_1@news.bluewin.ch...[color=green]
> > Is it possible to have several destructor in a same class?[/color]
> No - because the destructor is implictly called in most
> situations, and you wouldn't be able to specify which
> destructor to call.
>
> What is done instead is to:
> - Call explicitly a member function which will release
> resources or do any required specific operations.
> - Keep a flag or state variable in the object, which
> the destructor checks to know how it should behave.
> (this tends to be abused by novices, and is best avoided)
>
>
> --
> http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
> Brainbench MVP for C++ <> http://www.brainbench.com
>
>[/color]


Nicolas Pavlidis
Guest
 
Posts: n/a
#4: Jul 23 '05

re: C++ class


stephane wrote:
[color=blue]
> So there can be only one destructor.
>
> If I have declared in my class this for instance:
>
> ~Tvector(){delete[]m_values;}
>
> there is no ~Tvector(){} anymore?[/color]

No, if you do not provide a self defined destructor, the compiler
generates a destructor for you, which can be ok, but maybe the compiler
produces the wrong one, you gave a good example, if you would not
provied this destructor, the memory alocated by operator new[] for the
pointer m_values, will not be freed, at clean up.

The compiler it self only removes things that it has allocated / created.

HTH
Nicolas
Closed Thread