Does a C++ destructor always or only sometimes call data member destructors?

Posted by Magnus on Stack Overflow See other posts from Stack Overflow or by Magnus
Published on 2013-11-09T03:34:31Z Indexed on 2013/11/09 3:53 UTC
Read the original article Hit count: 252

Filed under:
|

I'm trying to validate my understanding of C++ destructors.

I've read many times that C++ supplies a default destructor if I don't write one myself. But does this mean that if I DO write a destructor that the compiler WON'T still provide the default cleanup of stack-allocated class fields?

My hunch is that the only sane behavior would be that all class fields are destroyed no matter what, whether I provide my own destructor or not. In which case the statement I've read so many times is actually a little misleading and could be better stated as:

"Whether or not you write your own destructor, the C++ compiler always writes a default destructor-like sequence to deallocate the member variables of your class. You may then specify additional deallocations or other tasks as needed by defining your own destructor"

Is this correct?

© Stack Overflow or respective owner

Related posts about c++

Related posts about destructor