Reference to non-static member function must be called(必须调用非静态成员函数的引用)
问题描述
我使用的是 C++(不是 C++11).我需要创建一个指向类中函数的指针.我尝试执行以下操作:
I'm using C++ (not C++11). I need to make a pointer to a function inside a class. I try to do following:
void MyClass::buttonClickedEvent( int buttonId ) {
// I need to have an access to all members of MyClass's class
}
void MyClass::setEvent() {
void ( *func ) ( int );
func = buttonClickedEvent; // <-- Reference to non static member function must be called
}
setEvent();
但是有一个错误:必须调用对非静态成员函数的引用".我应该怎么做才能指向 MyClass 的成员?
But there's an error: "Reference to non static member function must be called". What should I do to make a pointer to a member of MyClass?
推荐答案
问题在于 buttonClickedEvent 是一个成员函数,你需要一个指向 member 的指针才能调用它.
The problem is that buttonClickedEvent is a member function and you need a pointer to member in order to invoke it.
试试这个:
void (MyClass::*func)(int);
func = &MyClass::buttonClickedEvent;
然后当你调用它时,你需要一个 MyClass 类型的对象来这样做,例如 this:
And then when you invoke it, you need an object of type MyClass to do so, for example this:
(this->*func)(<argument>);
http://www.codeguru.com/cpp/cpp/article.php/c17401/C-Tutorial-PointertoMember-Function.htm
这篇关于必须调用非静态成员函数的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:必须调用非静态成员函数的引用
基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
