Friday 27 October 2017

c++ - How do virtual destructors work?

In C++, a trivial destructor
is a recursively defined concept -- it's a destructor that the compiler wrote for you
when every member of the class (and every base class) has a trivial destructor. (There's
a similar concept called the trivial
constructor.)



When an object with a nontrivial
destructor is included in an object (like the vector in your
example), then the destructor of the outside object (like your
Derived) in is no longer trivial. Even though you didn't write
destructor, the C++ compiler automatically wrote a destructor that calls the destructors
of any members that have
destructors.




So, even though you
didn't write anything, the caveats of writing a non-virtual destructor still
apply.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...