How destructors are called in class hierarchy? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How destructors are called in class hierarchy?

21st Nov 2016, 3:33 PM
Milan S. Magar
Milan S. Magar - avatar
3 Answers
+ 2
If your code is like this:- class A { ... }; Class B { ... }; class C : public B, public A { ... }; And when you make an object of class C, say C obj, then first the destructor of C will be called, then of A and at last B. It works opposite to that of the constructors.
22nd Nov 2016, 6:54 AM
james nyle
+ 1
The destructor is called whenever an object's lifetime ends, which includes program termination, for objects with static storage durationthread exit, for objects with thread-local storage duration(since C++11)end of scope, for objects with automatic storage duration and for temporaries whose life was extended by binding to a referencedelete-expression, for objects with dynamic storage durationend of the full expression, for nameless temporariesstack unwinding, for objects with automatic storage duration when an exception escapes their block, uncaught. The destructor may also be called directly, e.g. to destroy an object that was constructed using placement-new or through an allocator member function such as std::allocator::destroy(), to destroy an object that was constructed through the allocator. Note that calling a destructor directly for an ordinary object, such as a local variable, invokes undefined behavior when the destructor is called again, at the end of scope. In generic contexts, the destructor call syntax can be used with an object of non-class type; this is known as pseudo-destructor call: see member access operator.
21st Nov 2016, 3:53 PM
Matteo Tardelli
Matteo Tardelli - avatar
0
when the object's lifetime finishes
21st Nov 2016, 5:32 PM
atena mohammadi
atena mohammadi - avatar