Connecting Tech Pros Worldwide Help | Site Map

C++ class

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 12:50 AM
stephane
Guest
 
Posts: n/a
Default C++ class

Is it possible to have several destructor in a same class?



  #2  
Old July 23rd, 2005, 12:50 AM
Ivan Vecerina
Guest
 
Posts: n/a
Default 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, 12:50 AM
stephane
Guest
 
Posts: n/a
Default 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, 12:50 AM
Nicolas Pavlidis
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.