C++ standard at 12.4.2 states that
[...] The address of a destructor shall not be taken. [...]
However, one can without any complaints by the compiler take the address of a wrapper around a class destructor, like this:
struct Test { ~Test(){}; void destructor(){ this->~Test(); } }; void (Test::*d)() = &Test::destructor;
So what's the rationale behind forbidding to take the address of a destructor directly?
Use a protected destructor to prevent the destruction of a derived object via a base-class pointer. It limits access to the destuctor to derived classes. And it prevents automatic (stack) objects of class base.
A destructor takes no arguments and has no return type. Its address cannot be taken. Destructors cannot be declared const , volatile , const volatile or static . A destructor can be declared virtual or pure virtual .
The answer is yes. Destructor for each object is called. On a related note, you should try to avoid using delete whenever possible.
When something is created using dynamic memory allocation, it is the programmer's responsibility to delete it. So compiler doesn't bother. In the case where the destructor is declared private, an instance of the class can also be created using the malloc() function.
Constructors and destructors are somewhat special. The compiler often uses different conventions when calling them (e.g. to pass extra hidden arguments). If you took the address and saved it somewhere, the compiler would lose the information that the function is a constructor or destructor, and would not know to use the special conventions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With