Sign in to follow this  
Calin

Quick Distructor question (C++)

Recommended Posts

Calin    419
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(){ }

Share this post


Link to post
Share on other sites
SLWatmough    104
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.

Share this post


Link to post
Share on other sites
Calin    419
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.

Share this post


Link to post
Share on other sites
SiCrane    11839
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).

Share this post


Link to post
Share on other sites
Calin    419
Quote:

and then calls the destructors for all member variables


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this