Should C++ destructor be virtual?

In C++ speak interface is pure virtual class. Destructor is part of the interface and expected to implemented. Therefore destructor should be pure virtual.

Are destructors virtual?

Trivial destructor The destructor is not virtual (that is, the base class destructor is not virtual) All direct base classes have trivial destructors. All non-static data members of class type (or array of class type) have trivial destructors.

Can we have virtual destructor in C++ Mcq?

There is no concept of virtual constructor available in C++. However, virtual destructor is available in C++ language to maintain the destructor call from derived to base class.

Should base class destructor be virtual?

Deleting a derived class object using a pointer of base class type that has a non-virtual destructor results in undefined behavior. To correct this situation, the base class should be defined with a virtual destructor.

Can a destructor be pure virtual?

Yes, it is possible to have a pure virtual destructor. Pure virtual destructors are legal in standard C++ and one of the most important things to remember is that if a class contains a pure virtual destructor, it must provide a function body for the pure virtual destructor.

Why do we need destructor in C++?

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.

Can we have virtual destructor and pure virtual destructor?

Is it possible to have a virtual constructor and virtual destructor?

You can’t have virtual constructors, but a virtual destructor makes it possible to destroy an object through a base class pointer while calling the derived destructors apropriately. Class A’s destructor should’ve been marked as virtual. You only need to write virtual on the topmost class of the hierarchy.

Does B inherit constructors and destructors from a?

B does not inherit constructors from A; Unless B’s ctor explicitely calls one ofA’s ctor, the default ctor from A will be called automatically beforeB’s ctor body (the idea being that A needs to be initialized before B gets created). Destructors: B does not inherit A’s dtor;

Are destructors inherited?

Even though it’s rather clear from the posts that destructors seem to be inherited, I’m still puzzled by the fact that a user with 32k reputation would say its not. I wrote a little example that should clarify everyone’s mind:

Is the destructor virtual in 1) optional or mandatory?

So the virtual in 1) is “optional”, the base class destructor being virtual makes all child destructors virtual too. Show activity on this post.

What is the difference between a derived class and a destructor?

This is the main area where that difference manifests itself. What the C++ standard is essentially stating is that the destructor of a base class is not part of a derived class (it is not inherited in that sense). But the virtual-call mechanism is still required to work if destructors are involved (so in that sense, destructors are ‘inherited’)