C++ class

I

Ivan Vecerina

stephane said:
Is it possible to have several destructor in a same class?
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)
 
S

stephane

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?
 
N

Nicolas Pavlidis

stephane said:
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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,652
Latest member
Campbellamy

Latest Threads

Top