non-virtual dtor in class hierarchy – some scenarios

Background — How important are these scenarios? First off, tech quizzes are extremely important since you are judged just over a few questions. Second, these scenarios pop up by accidents, rather than be design, all the time in real projects. You better learn to deal with a drunken driver while on the road.

Q: what if Base and Derived dtor both non-virtual and an autoVar is destroyed?
%%A: see post on DCBC.

Q: What if Base dtor is not virtual but Derived is virtual, and a Derived auto variable is destroyed on the stack?
%%A: For an autoVariable that’s not deleted via a ptr, Derived ctor (virtual or not) runs, followed by Base dtor. Same DCBC

Note the well-known __undefinedBehavior__ affects delete only, not stack variables or static variables.

Note virtual keyword affects pointer variable. Non-ref variables aren’t affected.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s