I am trying to understand the underlying mechanism used to realize pointer to non-static member function. I am looking for an answer similar to how vtbl (virtual table for polymophism) works in the big picture, without worrying the details that could be different from compiler to compiler.
Example:
#include <stdio.h>
class A {
public:
int i;
int j;
void foo(void) { };
};
int main ()
{
int A::*ptr_j = &A::j;
void (A::*ptr_f)(void) = &A::foo;
printf("Foo::j pointer to data member %p\r\n", ptr_j);
printf("Foo::foo pointer to function member %p\r\n", ptr_f);
}
The result is
Foo::j
pointer to data member0x4
Foo::foo
pointer to function member0x804844c
From "The C++ Programming Language By Stroustrup",
A pointer to member...is more like an offset into a structure or an index into an array...
For a data member, I understand that Pointer-To-Member Foo::j
is more or less equivalent to offsetOf(Foo, j)
. The value when using gcc compiler in my host environment is 4, and it matches that offsetOf(Foo, j)
is 4.
For a function member, the returned value is 0x804844c
. And this is some address that belongs to global data area (where class is loaded?)
So my question is:
What is the "object" that is address 0x804844c
.
- It cannot be a simple
offsetOf()
, as this is a big offset. - It cannot be the address of vtbl (or address of an entry in vtbl) because I believe vbtl is an entity associated with instantiated object, not with the class.
- It cannot be the address where the function's implementation code is loaded. Because the same pointer can behave polymorphically when applied with a derived object.
Then what is the object at address 0x804844c
, and what is its role in translating the pointer-to-member-function into the actual function address when operator ->*
or .*
is applied?