DCBC — dtor execution order

DDDerived class dtor, which should not explicitly invoke C or B.
CCComponent (ie field) dtor
BBBase dtor
CCComponent of BBBase class

Compiler arranges these steps to execute in a single thread. See P277 ARM

A1: same DCBC according to EffC++. I probably tested it.

A3: See P60 [[safe c++]]. The exception triggers stack unwinding — C B C. Exact same sequence except the very first step i.e. the D is skipped. Derived dtor is skipped because Derived ctor didn’t complete.

The simple rule in this “exceptional” scenario is “whenever a ctor completes without throwing, its dtor would be executed.

A2: constructed First, so destructed Last. Remember dtor^ctor is ALWAYS reverse order

Quizzes:
Q1: but what if i invoke Derived virtual dtor through “delete”?
Q2: Virtual base?
Q3: what if the derived ctor throws exception after B and C completed? Which dtors will/won’t run and in what order?

(To aid blog search, D-C-B-C)

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s