The best answer to the question, why are destructors called from derivatives to the base?

Recently I attended an interview where a person asked me what the order of construction and destruction is. I explained that construction occurred from the foundation to the child and destruction from the child to the foundation.

The interviewer was fascinated by the fact that he knew if there was any particular reason for the destruction that occurred from the derivative to the base. I explained to him, but he was not convinced.

His thought was that if destroying a base class gives an exception, then as if the derived class knew that the object of the derived class was already destroyed.

He also said that a derived class contains members of a base class, so why can't we first cause the destruction of the base class?

I explained that after the destruction of the derived class, we cannot say that the object is completely destroyed.

I'm here? What is the best answer here?

+4
source share
3 answers

In C #, an object's construction follows this order:

  • Initializing Derived β†’ Base Data Elements
  • Execution of the base of constructors β†’ Derivatives.

Why?

  • Initialization follows Derived -> Base to avoid reinitializing members initialized differently in Derived from the base.
  • The line follows Base -> Derived, because the Derived constructor can rely on methods of the Base class.

Other languages ​​handle initialization differently, but the typical Base-> Derived construct.

Destruction comes from Derived -> Base.

Why?

  • Derivatives can still use the funds allocated by the base at the time of destruction.

    • If the base was first destroyed, these resources will no longer be available to Derived.
  • Each level of the hierarchy should be responsible for releasing any resources allocated by that level.

    • The database cannot release Derived Class resources that it does not know about.
    • Derivatives should also not be relevant for resources allocated by the database.
      • In a rigorous OOP, derivatives do not even need to know the details of these resources or how they are allocated or released.

To answer his specific questions:

  • Database Exception
    • Exceptions should not be thrown in destructors (in particular, in C # this can kill the garbage collector at least once)
  • Derived classes contain base class elements
    • An object has one instance of any data item. Not one per level hierarchy.
      Thus, we return to the fact that if the database frees the resources associated with the data item, it will no longer be available for use by the Derived class.

To your explanation of the argumentation order. If we assume that the order of Derived-> Base is correct, this explains why you should still call the base destructor, but not why Derived comes first.
If the problems with ordering were different and the destruction was completed by Base-> Derived, we could consider the object completely destroyed after the Derived destructor was completed, since the destruction would have occurred at all levels.

+1
source

You are absolutely right. I often asked this question when interviewing experienced C ++ programmers. A derived class must be built after the base class so that the constructor of the derived class can reference the data of the base class. For the same reason, the destructor of the derived class must run before the destructor of the base class. This is very logical: we build from the inside and destroy from the outside. If the base class destructor throws an exception, it cannot be caught by the derived class destructor. Also, an exception in the constructor of the base class is not handled by the constructor of the derived class. In general, exceptions should not be thrown from destructors.

+2
source

It is important to understand that the construction of the object occurs in stages. If you have a class B derived from A, you build B by first creating A and then turning A into B. Similarly, B is destroyed by first turning it into A and then destroying A. This provides a very consistent way of thinking about creating and destruction of the object.

0
source

All Articles