What is the difference between explicitly declaring a default destructor (using = default
), and not declaring anything ? Are there cases when it is required to explicitely define at least a default destructor ?
Please note that I am not speaking about needing to free some allocated memory, I'm asking about destructors that do nothing but the default
I could not came across a case where the former has a different behaviour than the latter. In both cases the compiler seems to generate a call to parents destructor or members destructors.
In those two cases, both behaviours (with or without explicitly defaulted destructor) seem equivalent :
// 1
struct A {
~A() { std::cout << "~A()\n"; }
};
struct B : public A {
// Comment this line out
~B() = default;
};
// 2
struct C {
~C() { std::cout << "~C()\n"; }
};
struct D {
// Comment this line out
~D() = default;
C c;
};
int main() {
B b;
D d;
return 0;
}