0
How destructors are called in class hierarchy?
3 Réponses
+ 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.
+ 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.
0
when the object's lifetime finishes