Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ overloaded method pointer [duplicate]

Tags:

c++

How do I get a method pointer to a particular overload of a method:

struct A {
    void f();
    void f(int);
    void g();
};

I know that

&A::g

is a pointer to g. But how do I get a pointer to f or f(int)?

like image 917
Neil G Avatar asked Dec 06 '10 08:12

Neil G


2 Answers

(void (A::*)()) &A::f
(void (A::*)(int)) &A::f

function pointers and member function pointers have this feature - the overload can be resolved by to what the result was assigned or cast.

If the functions are static, then you should treat them as ordinary functions:

(void (*)()) &A::f;
(void (*)(int)) &A::f;

or even

(void (*)()) A::f;
(void (*)(int)) A::f;
like image 95
Armen Tsirunyan Avatar answered Oct 31 '22 19:10

Armen Tsirunyan


You just have to cast the result of &A::f in order to remove the ambiguity :

static_cast<void (A::*)()>(&A::f); // pointer to parameterless f
static_cast<void (A::*)(int)>(&A::f); // pointer to f which takes an int
like image 9
icecrime Avatar answered Oct 31 '22 20:10

icecrime