Hey i’m trying to figure out Function pointers and how to pass them around/ declare them, but i’m having a little trouble passing a pointer in my Button class constructor and setting it’s member function pointer too the passed pointer.
- when i write Button(Func1) Button1 it says expected a ‘;’
- when i write Button(Func1); it says no default constructor for Button
- when i write Button(&Func1); it says Func1 requires an initializer
- when i write Button(&Func1()) Button1; it says expression must be lvalue or function designator
What am i doing wrong?
void Func1(){std::cout << "This is a function\n";};
void Func2(){std::cout << "This is another function\n";};
class Button
{
private:
void (*Func)(void);
public:
void Activate(){ Func() ;};
Button( void (*Function)(void)){
this->Func = Function;};
};
Button(&Func1) Button1;
Button(&Func2) Button2;
Button1.Activate();
Button2.Activate();
This code has wrong syntax:
It should be:
And function pointer declaration:
Leave without void in parameters:
Edit: Working example at ideone.