When returning a member function pointer to a class within one of that class's member functions I still have to specify the class. I cannot simply take the address. For example, this code works fine:
class Foo {
public:
void func(int param) { cout << param << endl; }
void (Foo::*getPointer())(int) { return &Foo::func; }
};
But if in getPointer
I try to simply do: return &func
I get this error:
prog.cpp: In member function '
void (Foo::* Foo::getPointer())(int)
':
prog.cpp:8:43: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&Foo::func
' [-fpermissive]void (Foo::*getPointer())(int) { return &func; }
Why must I specify the class when that's the context that I am within?
Pointers and pointer to members are distinct types, we can see that from section the draft C++ standard section 3.9.2
[basic.compound] which includes a compound type for pointer as well as pointer to non-static class member and notes:
Static class members are objects or functions, and pointers to them are ordinary pointers to objects or functions
This issue with this is I think well described in this quote in an answer from Johannes from the Annotated C++ Reference Manual(ARM):
Note that the address-of operator must be explicitly used to get a pointer to member; there is no implicit conversion ... Had there been, we would have an ambiguity in the context of a member function ... For example,
void B::f() { int B::* p = &B::i; // ok p = B::i; // error: B::i is an int p = &i; // error: '&i'means '&this->i' // which is an 'int*' int *q = &i; // ok q = B::i; // error: 'B::i is an int q = &B::i; // error: '&B::i' is an 'int B::*' }
In particular these lines:
int B::* p = &B::i; // OK
and:
p = &i; // error: '&i'means '&this->i' which is an 'int*'
demonstrate the difference between the qualified and the unqualified name.
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