为什么即使在 class 内部获取成员函数指针值也需要 class 名称限定?
Why does taking a member function pointer value requires class name qualification even from inside of the class?
当在 class 的成员函数之一中返回指向 class 的成员函数指针时,我仍然必须指定 class。我不能简单地接受地址。例如,this code works fine:
class Foo {
public:
void func(int param) { cout << param << endl; }
void (Foo::*getPointer())(int) { return &Foo::func; }
};
但是如果在 getPointer
中我尝试简单地做:return &func
我得到这个错误:
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; }
为什么我必须指定 class,因为这是我所在的上下文?
(...有一个错误的答案...)
在以下上下文中看起来更奇怪:
class Foo {
public:
virtual void func(int param) { cout << param << endl; }
void call(int x) { Foo::func(x); }
void (Foo::*getPointer())(int) { return &Foo::func; }
};
class Bar : public Foo {
public:
void func(int param) override { cout << "Hello world!" << endl; }
};
int main() {
Foo *a = new Bar();
auto p = a->getPointer();
(a->*p)(4);
a->call(4);
return 0;
}
输出为
Hello world
4
调用Foo::func
是在Foo
中调用func
class而调用&Foo::func
是虚拟调用。
指针和指向成员的指针是不同的类型,我们可以从草案C++标准部分3.9.2
[basic.compound]中看到,其中包括指针的复合类型以及指向非静态 class 成员 和注释的指针:
Static class members are objects or functions, and pointers to them
are ordinary pointers to objects or functions
我认为这个问题在 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::*'
}
特别是这些行:
int B::* p = &B::i; // OK
和:
p = &i; // error: '&i'means '&this->i' which is an 'int*'
演示合格名称和非合格名称之间的区别。
当在 class 的成员函数之一中返回指向 class 的成员函数指针时,我仍然必须指定 class。我不能简单地接受地址。例如,this code works fine:
class Foo {
public:
void func(int param) { cout << param << endl; }
void (Foo::*getPointer())(int) { return &Foo::func; }
};
但是如果在 getPointer
中我尝试简单地做:return &func
我得到这个错误:
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; }
为什么我必须指定 class,因为这是我所在的上下文?
(...有一个错误的答案...)
在以下上下文中看起来更奇怪:
class Foo {
public:
virtual void func(int param) { cout << param << endl; }
void call(int x) { Foo::func(x); }
void (Foo::*getPointer())(int) { return &Foo::func; }
};
class Bar : public Foo {
public:
void func(int param) override { cout << "Hello world!" << endl; }
};
int main() {
Foo *a = new Bar();
auto p = a->getPointer();
(a->*p)(4);
a->call(4);
return 0;
}
输出为
Hello world
4
调用Foo::func
是在Foo
中调用func
class而调用&Foo::func
是虚拟调用。
指针和指向成员的指针是不同的类型,我们可以从草案C++标准部分3.9.2
[basic.compound]中看到,其中包括指针的复合类型以及指向非静态 class 成员 和注释的指针:
Static class members are objects or functions, and pointers to them are ordinary pointers to objects or functions
我认为这个问题在 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::*' }
特别是这些行:
int B::* p = &B::i; // OK
和:
p = &i; // error: '&i'means '&this->i' which is an 'int*'
演示合格名称和非合格名称之间的区别。