It is still unclear.
The "address" of a virtual function by itself (ie, without an instance of the object) isn't
a real memory address, the reason being that:
1 2 3 4 5 6 7 8 9 10 11
|
struct Base {
virtual void foo();
};
struct Intermediate : Base {
virtual void foo();
};
struct Derived : Intermediate {
virtual void foo();
};
|
when you say "&Derived::foo", "&Intermediate::Foo", or "&Base::foo", the compiler
does not know which function you want the address of, despite the qualification.
Because, this has to work:
1 2 3 4 5
|
int main() {
Derived* d = new Derived;
void (Base::*pFn)() = &Base::foo; // pointer to Base::foo
d->*pFn(); // Calls Derived::foo by pointer
}
|
On line 3, the compiler does not know how you are going to use pFn to know that
you really meant the address of "Base::foo" or if you meant one of its overrides.
On gcc, you'll find that sizeof( pFn ) is in fact more than 32 bits even on a 32-bit
system; the reason is because the pointer encodes two additional pieces of
information: it encodes the fact that the pointer is a pointer to a
virtual function
and it encodes the index of the virtual function into the vtable. (In my above bad
example, foo() is the 0th virtual function the compiler encountered in the class).