test when c 2b 2b destructor is called

Solutions on MaxInterview for test when c 2b 2b destructor is called by the best coders in the world

showing results for - "test when c 2b 2b destructor is called"
Elliot
05 Jan 2020
1// order_of_destruction.cpp
2#include <cstdio>
3
4struct A1      { virtual ~A1() { printf("A1 dtor\n"); } };
5struct A2 : A1 { virtual ~A2() { printf("A2 dtor\n"); } };
6struct A3 : A2 { virtual ~A3() { printf("A3 dtor\n"); } };
7
8struct B1      { ~B1() { printf("B1 dtor\n"); } };
9struct B2 : B1 { ~B2() { printf("B2 dtor\n"); } };
10struct B3 : B2 { ~B3() { printf("B3 dtor\n"); } };
11
12int main() {
13   A1 * a = new A3;
14   delete a;
15   printf("\n");
16
17   B1 * b = new B3;
18   delete b;
19   printf("\n");
20
21   B3 * b2 = new B3;
22   delete b2;
23}
24
25Output: A3 dtor
26A2 dtor
27A1 dtor
28
29B1 dtor
30
31B3 dtor
32B2 dtor
33B1 dtor
34