Quick Distructor question (C++)

Started by
4 comments, last by Calin 18 years, 4 months ago
If I have a class Apple and I define my own destructor withiout passing anything inside it, will the compiler use the default destructor or will it consider that I have defined my own destructor and use that even if it is empty? I mean this situation: Apple::~Apple(){ }

My project`s facebook page is “DreamLand Page”

Advertisement
It will use your destructor, which will do the exact same thing as the default destructor.
The compiler in c++ will create 3 things with respect to classes if the programmer doesn't explicitly do it already.

1) A default constructor
2) A copy constructor
3) A destructor.

Also, if you create a constructor which accepts parameters such as...

Apple::Apple( const CColor& colorOfApple )
{
}

...then the compiler will not make a default constructor.

The copy constructor is one of those things that unless you really know what your doing, your probably best leaving to the compiler. It only really needs to be added if you need deep copying of objects.

And onto the destructor...I wasn't aware that you could have parameters passed into the destructor anyway.

Unless marked virtual they will behave exaclty how you would imagin they would behave.

Anyway...hope that cleared up things.
The default distructor destroys all member variables.
However I thought if you leave your destructor empty then nothing would be deleted at the end of the application because the destrutor doesn't contain any instruction on how to proceed when the life of the class ends.
I'm not arguing with you SiCrane, I'm just trying to understand how things work.

My project`s facebook page is “DreamLand Page”

The destructor does whatever is in the body of the destructor and then calls the destructors for all member variables (if any) and then the destructor for all base classes (if any).
Quote:
and then calls the destructors for all member variables


I didn't knew this one. Thanks for the clarification.

My project`s facebook page is “DreamLand Page”

This topic is closed to new replies.

Advertisement