This question already has answers here:
Why do we need a pure virtual destructor in C++?
(11 answers)
Closed 2 years ago.
What is the application of virtual and pure virtual destructor in C++? What is the scenario where I would have to use a virtual destructor instead of a normal destructor?
When a pointer to a base class object is deleted, the compiler calls the corresponding destructor based on the actual type of object the pointer refers to.
If the base class destructor is not a virtual function, the compiler will automatically call the destructor of the base class when the base class pointer to the derived class object is deleted, without considering whether the actual object is an object of the base class.This can lead to memory leaks.
Related
This question already has answers here:
What is a vtable in C++ [duplicate]
(3 answers)
Why do we need a virtual table?
(5 answers)
How are virtual functions and vtable implemented?
(12 answers)
Closed 5 years ago.
What is the use of vtable (or why is vtable required ) in case of virtual inheritance ? what does this vtable points to in this case.
example:
class A
{
void show()
{ }
};
class B : virtual A
{
void disp()
{ }
};
In the above example the size of class B is 8 bytes. which means class B has vptr pointing to a Vtable. What does this vtable point to .
A vtable is the most common way of implementing the virtual keyword in C++ -- any class that uses the virtual keyword will have a vtable created for it and every instance of that class will contain a pointer to that (single) vtable. The vtable contains information on the dynamic class of the object (to support dynamic_cast and typeinfo) as well as information as to where virtual base classes and functions of the class are located.
In this specific case, the vtable for B will likely contain just dynamic class info, as A has no data members or virtual functions.
This question already has answers here:
Why do we need a pure virtual destructor in C++?
(11 answers)
Closed 7 years ago.
As I know in cpp, when we delete an object or when the main finishes, the destructors of all objects will be called. For those objects whose type are class child, the destructors of class child will be called first then the distructors of class parent will be called.
Now I am confused. Because if a pure virtual destructor is allowed, how could it be called when we delete an object of class child? Doesn't it call the destructor of class parent which is pure virtual?
Yes, the destructor of the base class is called. This means it must have an implementation. It is possible to provide an implementation for any pure virtual function, including the destructor. For example:
struct foo
{
virtual ~foo() = 0; // pure virtual dtor
};
foo::~foo() {} // implementation
The use-case of a pure virtual destructor is to ensure a class without any other pure virtual methods cannot be instantiated.
This question already has answers here:
Does delete work with pointers to base class?
(2 answers)
Closed 7 years ago.
Does it cause UB if we define a virtual destructor? For intance:
struct A{ virtual ~A(){ } };
struct B : A { };
A *a = new B;
int main()
{
delete a; //UB?
}
coliru
It is fine precisely because the destructor is virtual — otherwise it would have been UB.
In other words, if you want to delete objects of derived type, through pointers of base type, then the destructor of base class must be virtual, else it would be UB. That ensures that the correct destructor (i.e the destructor of derived) is invoked — that is called runtime polymorphism.
"Does it cause UB if we define a virtual destructor?"
No that's just fine as the destructor was declared virtual. Stepping up the vtable and calling ~B first will be handled by delete.
Its fine, so long as the destructor is virtual.
If the destuctor is not, it will not know to delete the members of the subclass.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Do I need to explicitly call the base virtual destructor?
Lets say you have the following:
class ParentClass {
...
virtual ~ParentClass();
and
class ChildClass {
...
virtual ~ChildClass();
Which of the destructors would be called? Would both the parent and child's destructors be called? Currently don't have C++ compilers set up on my computer.
If ChildClass is derived from ParentClass then the derived destructor is called first, followed by the parent class. As it stands in your code, ChildClass does not inherit from ParentClass
Yes, both constructors are call: construction and destruction are symmetric: All subobjects get destroyed in exactly the opposite order they were created. For the order of destruction it doesn't matter if the destructor is virtual. The only impact of virtual vs. non-virtual destructors is when deleteing an object of a dreived type using a pointer to a base: This results in undefined behavior if the destructor of the base isn't virtual.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calling virtual functions inside constructors
in C++, An object of class B derived from class A, in C++ the c’tor of A is invoked before the c’tor of B , why ?
And what happens if A’s C’tor invokes a virtual function? does it invoke A's virtual function of B's ?
Most of your questions, if not all, are covered by the C++ FAQ.
E.g. see the FAQ "When my base class's constructor calls a virtual function on its this object, why doesn't my derived class's override of that virtual function get invoked?".
It is always a good idea to read the FAQ before asking.