必须调用对非静态成员函数的引用
Reference to non-static member function must be called
我使用的是C++(而不是C++11)。我需要制作一个指向类内函数的指针。我试着做以下事情:
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的成员?
问题是buttonClickedEvent
是一个成员函数,您需要一个指向成员的指针才能调用它。
试试这个:
void (MyClass::*func)(int);
func = &MyClass::buttonClickedEvent;
然后,当您调用它时,您需要一个类型为MyClass
的对象来执行此操作,例如this
:
(this->*func)(<argument>);
http://www.codeguru.com/cpp/cpp/article.php/c17401/C-Tutorial-PointertoMember-Function.htm
您可能想看看https://isocpp.org/wiki/faq/pointers-to-members#fnptr-vs memfnptr类型,尤其是[33.1]"指针到成员函数"的类型与"指针到函数"的不同吗
您只需要在函数调用后添加括号,并在需要时传递参数
相关文章:
- 区分接受常量参数的函数引用/指针和与函数参数同名的非常量参数
- 类 Referention 中C++回调函数引用非静态函数
- 使用函数引用指向节点的指针删除链表中的节点?
- 解释通过从函数引用返回数组的语法
- "Class1"类"Class2"对象作为私有数据成员。如何通过"Class 2"函数引用"Class1"对象?
- 使用默认构造函数引用成员变量初始化错误
- 无法调用函数引用 c++
- 使用 decltype(this) 获取函数引用
- 我应该如何定义返回指针的函数?(引用指针与指针指针)
- 从内联函数引用文件静态变量
- Boost::将sigaction函数引用绑定到实例
- 一种比函数引用更有效的方法
- 奇怪的未定义函数引用,函数调用C++不存在
- 是否可以检测绑定成员函数引用的对象是否被删除或销毁
- 构造函数引用参数导致seg错误
- 是否可以使函数模板从函数引用中获取“decltype”
- 无法让 Lua 函数引用"self"
- 如何解析变量和函数引用(Linker & Compiler)?
- 从不同模块调用函数 - 引用错误
- 如何在主函数中连接到数据库,然后从其他函数引用它