Quick Distructor question (C++)
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(){
}
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.
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.
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.
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).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement