Connecting Tech Pros Worldwide Help | Site Map

C++ class

  #1  
Old July 23rd, 2005, 01:50 AM
stephane
Guest
 
Posts: n/a
Is it possible to have several destructor in a same class?


  #2  
Old July 23rd, 2005, 01:50 AM
Ivan Vecerina
Guest
 
Posts: n/a

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


  #3  
Old July 23rd, 2005, 01:50 AM
stephane
Guest
 
Posts: n/a

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]


  #4  
Old July 23rd, 2005, 01:50 AM
Nicolas Pavlidis
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Old template class works in VC++ Not in C++ Builder 5 Hamilton Woods answers 3 December 1st, 2006 06:05 PM
Page_load not called in base class Andy answers 5 November 19th, 2005 08:01 PM
access public function of derived class through base class pointer Banaticus Bart answers 9 July 22nd, 2005 11:40 AM
Correct syntax for definitions of inner classes of a template class. Oplec answers 1 July 19th, 2005 08:41 PM
Getting the super class via the super() function Fernando Rodriguez answers 2 July 18th, 2005 06:57 AM