Function pointer to a member function

I would like to set a pointer to a function as a member of a class that is a pointer to another function in the same class. The reasons why I do this are complex.

In this example, I would like the result to be "1"

class A { public: int f(); int (*x)(); } int A::f() { return 1; } int main() { A a; ax = af; printf("%d\n",ax()) } 

But this fails when compiling. Why?

+77
c ++ oop function-pointers
Mar 08
source share
6 answers

The syntax is invalid. A member pointer is another type category from a regular pointer. The element pointer must be used together with its class object:

 class A { public: int f(); int (A::*x)(); // <- declare by saying what class it is a pointer to }; int A::f() { return 1; } int main() { A a; ax = &A::f; // use the :: syntax printf("%d\n",(a.*(ax))()); // use together with an object of its class } 

ax does not yet say which object the function should be called on. It just says that you want to use a pointer stored in object a . Turning a a different time when the left operand to the operator .* Tells the compiler which object to call the function on.

+132
Mar 08 '10 at 15:57
source share

int (*x)() not a pointer to a member function. A pointer to a member function is written as follows: int (A::*x)(void) = &A::f; .

+21
Mar 08 '10 at 15:59
source share

Call Participant Function in a String Command

 #include <iostream> #include <string> class A { public: void call(); private: void printH(); void command(std::string a, std::string b, void (A::*func)()); }; void A::printH() { std::cout<< "H\n"; } void A::call() { command("a","a", &A::printH); } void A::command(std::string a, std::string b, void (A::*func)()) { if(a == b) { (this->*func)(); } } int main() { A a; a.call(); return 0; } 

Pay attention to (this->*func)(); and a way to declare a function pointer with the class name void (A::*func)()

+13
May 28 '16 at 14:39
source share

You need to use a pointer to a member function, not just a pointer to a function.

 class A { int f() { return 1; } public: int (A::*x)(); A() : x(&A::f) {} }; int main() { A a; std::cout << (a.*ax)(); return 0; } 
+9
Mar 08 '10 at 15:59
source share

Although this is based on Sterling's answers elsewhere on this page, I had a use case that was not completely resolved by them; for a vector of function pointers, do the following:

 #include <iostream> #include <vector> #include <stdio.h> #include <stdlib.h> class A{ public: typedef vector<int> (A::*AFunc)(int I1,int I2); vector<AFunc> FuncList; inline int Subtract(int I1,int I2){return I1-I2;}; inline int Add(int I1,int I2){return I1+I2;}; ... void Populate(); void ExecuteAll(); }; void A::Populate(){ FuncList.push_back(&A::Subtract); FuncList.push_back(&A::Add); ... } void A::ExecuteAll(){ int In1=1,In2=2,Out=0; for(size_t FuncId=0;FuncId<FuncList.size();FuncId++){ Out=(this->*FuncList[FuncId])(In1,In2); printf("Function %ld output %d\n",FuncId,Out); } } int main(){ A Demo; Demo.Populate(); Demo.ExecuteAll(); return 0; } 

Something like this is useful if you are writing a shell with indexed functions that should be married to parameter syntax and hint tips, etc. Perhaps this is also useful on the menu.

+3
Apr 12 '17 at 22:00
source share

Unfortunately, you cannot convert an existing pointer to a member function to a simple pointer to a function, but you can create an adapter function template in a fairly simple way that wraps a pointer to a member function known at compile time into a regular function like this:

 template <class Type> struct member_function; template <class Type, class Ret, class... Args> struct member_function<Ret(Type::*)(Args...)> { template <Ret(Type::*Func)(Args...)> static Ret adapter(Type &obj, Args&&... args) { return (obj.*Func)(std::forward<Args>(args)...); } }; template <class Type, class Ret, class... Args> struct member_function<Ret(Type::*)(Args...) const> { template <Ret(Type::*Func)(Args...) const> static Ret adapter(const Type &obj, Args&&... args) { return (obj.*Func)(std::forward<Args>(args)...); } }; 

 int (*func)(A&) = &member_function<decltype(&A::f)>::adapter<&A::f>; 

Note that an instance of A must be provided to call a member function.

+1
Aug 20 '19 at 12:25
source share



All Articles