Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Reference to non-static member function must be called

Writer Olivia Zamora

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?

2

2 Answers

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

You may want to have a look at , especially [33.1] Is the type of "pointer-to-member-function" different from "pointer-to-function"?

0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy