Assume I have
struct X {
~X() {}
};
What's the type of and how do I get the member function pointer of X::~X()
in C++03?
I don't want to actually call it, just use in SFINAE to figure if there exists a destructor for a given type.
To access a member function by pointer, we have to declare a pointer to the object and initialize it (by creating the memory at runtime, yes! We can use new keyboard for this). The second step, use arrow operator -> to access the member function using the pointer to the object.
1) Pointer declarator: the declaration S* D; declares D as a pointer to the type determined by decl-specifier-seq S . 2) Pointer to member declarator: the declaration S C::* D; declares D as a pointer to non-static member of C of type determined by decl-specifier-seq S . nested-name-specifier.
A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete . A destructor has the same name as the class, preceded by a tilde ( ~ ). For example, the destructor for class String is declared: ~String() .
A pointer to a member can be declared and used as shown in the following code fragment: typedef int X::*my_pointer_to_member; typedef void (X::*my_pointer_to_function) (int); int main() { my_pointer_to_member ptiptr = &X::a; my_pointer_to_function ptfptr = &X::f; X xobject; xobject.*ptiptr = 10; cout << "The value of a ...
You can't get the function pointer of a destructor nor a constructor. Nevertheless a destructor always exist for a type, and you can't detect if its private
with as access specifiers are not considered by SFINAE.
On the subject of invoking what would be the destructor of a scalar type, the standard says [class.dtor]/16:
[Note:the notation for explicit call of a destructor can be used for any scalar type name (5.2.4). Allowing this makes it possible to write code without having to know if a destructor exists for a given type. For example,
typedef int I;
I* p;
p->I::~I();
—end note]
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