必须调用对非静态成员函数的引用 [英] Reference to non-static member function must be called

查看:7313
本文介绍了必须调用对非静态成员函数的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++(不是C ++ 11)。我需要一个指向一个类中的函数的指针。我尝试做以下操作:

  void MyClass :: buttonClickedEvent(int buttonId){
//我需要对MyClass类的所有成员的访问
}

void MyClass :: setEvent(){

void(* func)(int);
func = buttonClickedEvent; //< - 对非静态成员函数的引用必须调用

}

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 p>

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();

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?

解决方案

The problem is that buttonClickedEvent is a member function and you need a pointer to member in order to invoke it.

Try this:

void (MyClass::*func)(int);
func = &MyClass::buttonClickedEvent;

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

这篇关于必须调用对非静态成员函数的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆