引用成员函数?
我最近发现 C++ 中有一个函数引用的概念:)。 因此存在函数指针和成员函数指针不同的类型。问题出现了。是否有“引用成员函数”的概念?
我尝试编译以下代码,但 GCC 3.4.6 给出错误。
#include <iostream>
using namespace std;
class A {
public:
virtual void Af() const {
cout << "A::Af()" << endl;
}
};
int main() {
typedef void (A::& MemFnc)() const;
MemFnc mf = &A::Af;
A a;
(a.*mf)();
return 0;
}
I recently find out that there is a reference-to-function concept in C++ :).
So as there are pointer-to-function and pointer-to-member-function different types. The question arises. Is there a "reference-to-member-function" concept?
I tried to compile the following code, but GCC 3.4.6 gives an error.
#include <iostream>
using namespace std;
class A {
public:
virtual void Af() const {
cout << "A::Af()" << endl;
}
};
int main() {
typedef void (A::& MemFnc)() const;
MemFnc mf = &A::Af;
A a;
(a.*mf)();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C++ 中不存在所谓的对成员的引用。
语言规范在注释中明确指出(§8.3.3/3 - 2003),
There is no such a thing called reference to member in C++.
The language specification explicitly says in a note (§8.3.3/3 - 2003) that,
不,不可能引用成员函数。
从某种意义上说,取消引用指向成员函数的指针的结果可以作为一个结果,但是您可以对该结果执行的唯一操作就是在其上调用函数调用运算符,按照
5.5[expr.mptr.oper]/6
。没有其他的事情是允许的。No, references to member functions are not possible.
In some sense, the result of dereferencing a pointer to a member function could serve as one, but the only thing you can do with that result is to invoke a function call operator on it, per
5.5[expr.mptr.oper]/6
. Nothing else is allowed.没有对成员函数的引用。
There is no reference to member function.