Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax in C++ for member function returning a function pointer

Tags:

c++

I have the following program which I'd like to get it fixed. Not sure how to make it syntactically correct.

class A{
    void f(){};
    void (A::*)()get_f(){return &A::f;}
};

Also, I would like eventually move the function definition outline like the following.

void A::(*)()A::get_f(){
    return &A::f;
}

What the correct syntax here too?

Thanks a lot for your help!

like image 470
Qiang Li Avatar asked Nov 11 '11 23:11

Qiang Li


1 Answers

Like this:

class A{
    void f(){};
    void (A::*get_f())() {return &A::f;}
};

and similarly:

void (A::* A::get_f())(){
    return &A::f;
}

See it in action on ideone. Note that using it is just the same as with typedef (in the other answers).

BTW, for extra points and vomit (ha, ha):

void (A::* (A::* get_get_f())())();
like image 170
jpalecek Avatar answered Sep 29 '22 09:09

jpalecek