Difference between virtual ~T(){} and virtual ~T() = default; [duplicate] - c++

This question already has answers here:
How is "=default" different from "{}" for default constructor and destructor?
(3 answers)
Closed 7 years ago.
When writing a virtual destructor, is there any functional or outwardly-discernible difference between having
virtual ~T() = default;
over
virtual ~T() {}
They both seem to have the same affect on anything from type_traits I could think to test with.

They are effectively the same. While there is a difference with non-virtual destructors, once you stick virtual in there, it cannot be trivial anymore.
This is not the only time that = default leads to the generation of a non-trivial special member function. For example, if you have a member that has a non-trivial destructor, then using = default will not cause the creation of a trivial destructor for the containing type, even with a non-virtual destructor.
You should use = default anyway, just to make it clear what your intentions are.

Related

Use of Virtual Destructor in C++ [duplicate]

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.

why is it possible to make the destructor pure virtual [duplicate]

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.

Can we perform deleting object through a pointer to one of its base classes? [duplicate]

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.

When the destructor of a subclass is called, will the destructor of a parent class also be called? [duplicate]

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.

Binding within Constructors within Constructors [duplicate]

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.