According to following test this:
std::cout << std::is_member_function_pointer<int A::*()>::value << std::endl;
Is not member function pointer, but regular function, while this:
std::cout << std::is_member_function_pointer<int (A::*)()>::value << std::endl;
evaluates to true. I tried both with gcc & msvc. What is difference between these two declarations? Are these results correct? Why are parenthesis around A::*
important?
First, because static member functions are not attached to an object, they have no this pointer! This makes sense when you think about it -- the this pointer always points to the object that the member function is working on. Static member functions do not work on an object, so the this pointer is not needed.
The this pointer is a pointer accessible only within the nonstatic member functions of a class , struct , or union type. It points to the object for which the member function is called. Static member functions don't have a this pointer.
Using a pointer-to-member-function to call a function Calling the member function on an object using a pointer-to-member-function result = (object. *pointer_name)(arguments); or calling with a pointer to the object result = (object_ptr->*pointer_name)(arguments);
Non-member Function: The function which is declared outside the class is known as the non-member function of that class. Below is the difference between the two: The member function can appear outside of the class body (for instance, in the implementation file).
int A::*()
is type of function, which returns A
's member with type int
, takes no arguments. So it's not member function pointer, even not function pointer.
std::cout << std::is_member_function_pointer<int A::*()>::value << std::endl; // 0
std::cout << std::is_pointer<int A::*()>::value << std::endl; // 0
std::cout << std::is_function<int A::*()>::value << std::endl << std::endl; // 1
And parentheses change the precedence, int (A::*)()
is type of A
's member function pointer, which returns int
and takes no arguments.
The differences of differently parenthesized type expressions, stem from the operator precedence.
Here is one way to get a more detailed, descriptive specification of a type:
C:\my\forums\so\120> echo struct A{}; using T = int A::*(); T o; int x = o; >1.cpp C:\my\forums\so\120> g++ -c 1.cpp 1.cpp:1:48: error: invalid conversion from 'int A::* (*)()' to 'int' [-fpermissive] struct A{}; using T = int A::*(); T o; int x = o; ^ C:\my\forums\so\120> _
So, we see that a variable of type int A::*()
has the type int A::* (*)()
.
EDIT: I cannot delete this post while it's marked as solution, so for the record: in the above code o
is not a variable. Instead it's a function declaration. int A::*()
is directly a function type, namely a function returning a data member pointer.
Now heading for coffee…
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