The this
pointer is not stored along a pointer to member (member function pointers are a special case of this). If you just do
void (MyObject::*f)( int, int ) = &MyObject::method_that_takes_two_ints;
then what is stored is just the information which member function should be called on a objet which you later have to provide. If you want to call it, you have to pass an object along, where the compiler will get the this
pointer from.
MyObject o; (o.*f)(1, 2);
A member function pointer is just a member pointer whose type (that's pointed to) is a function type. The Standard says that member function pointers do not have their own "member function type" that they point to and that would somehow include the this pointer type.
int main() {
typedef void fun() const;
fun MyObject::*mem_function_ptr =
&MyObject::const_method_that_takes_two_ints;
}
fun
in that code is the function type. The type that a "normal" function has. A pointer-to-function, as opposed to a member-function-pointer, is just a pointer to a function having that type:
void foo() { cout << "hello"; }
int main() {
typedef void fun();
fun * f = &foo;
}
While a pointer-to-member-function has the additional member-pointer level on-top of that function type.
Something about the this
pointer and how it relates to the object that it points to (not technical, just the theoretical stuff):
Each member function has a hidden parameter called the implicit object parameter
which has type MyObject&
or MyObject const&
depending on whether you have a const or nonconst member function. The object that you call the member function on, o
, is the implied object argument
, which is passed to the parameter. In the theory of the standard that make up the rules that describe how member functions are called, the implicit object parameter is a first hidden parameter. That's conceptual, and doesn't mean it's the real case in implementations. The implied object argument is then bound to that implicit object parameter, possibly causing implicit conversions (so if you call a const member function on a non-const object, a qualification conversion converts from MyObject
to MyObject const&
. That is what makes non-const functions a better choice than const functions to call, for a non-const object). For example, one can say in this code:
struct A {
operator int() const { return 0; }
};
int main() {
A a;
int i = a; // implicit conversion using the conversion function
}
That the implied object argument a
of type A
is bound to the implicit object parameter of type A const&
, whose object is then pointed to by the this
pointer having the type A const*
here. Important to note is that the implicit object parameter is only a theoretical construct, to formalize how the rules for calling a member function are made up (and constructors do not include them), while the this pointer is actually existent. this
is a pointer, because when this
was introduced, C++ didn't have references yet.
I hope that will help you understand the matter.