Reputation: 335
How do I get the member function pointer of a destructor?
here some-one answered that we can't get the function pointer of a destructor then how the virtual mechanism work in the below code. Is virtual constructor is saved in the virtual table or not?? if not then how virtual mechanism work in the case of virtual destructor?
#include<stdio.h>
class Base
{
public:
Base()
{
printf("C-Base\n");
}
virtual ~Base()
{
printf("Base\n");
}
};
class Derived:public Base
{
public:
Derived()
{
printf("C-DErived\n");
}
~Derived()
{
printf("DErived\n");
}
};
int main()
{
Base *b=new Derived();
delete b;
}
in this code if we not use virtual in Base class Destructor Derived class destructor will not called.
Upvotes: 4
Views: 283
Reputation: 320671
When someone says that you can't get a pointer to the destructor it means that there is no source-code-level syntax for this. But under the hood the destructor is still an ordinary function, which is indeed typically accessed through a pointer stored in the virtual table.
In other words, it is specifically you, who can't obtain such a pointer. The compiler itself has no problems obtaining it. Quod licet Iovi non licet bovi.
Upvotes: 10
Reputation: 72054
Just because you can't get a pointer to the destructor, doesn't mean the compiler can't for its own internal, nefarious purposes.
(As a side note, the compiler often generates more than one version of the destructor, with more than one entry in the vtable, which makes it incompatible with normal function pointers.)
Upvotes: 5