Failed to pass function pointer between classes

I am trying to pass a pointer to a function that is defined in one class in another class. After much research, I believe my syntax is correct, but I still get compiler errors. Here is some code that demonstrates my problem:

class Base { public: BaseClass(); virtual ~BaseClass(); }; class Derived : public Base { public: // assign strFunction to the function pointer passed in Derived(string (*funPtr)(int)) : strFunction(funPtr); ~Derived(); private: // pointer to the function that is passed in string (*strFunction)(int value); }; class MainClass { public: MainClass() { // allocate a new Derived class and pass it a pointer to myFunction Base* myClass = new Derived(&MainClass::myFunction); } string myFunction(int value) { // return a string } }; 

When I try to compile this code, I get an error

error: there is no corresponding function to call in "Derived :: Derived" (string (MainClass :: *) (int)) '

followed by

Note: Candidates: Derivatives :: Derivatives (string (*) (int))

Any idea what I can do wrong?

+6
c ++
source share
9 answers

your syntax is correct for a C function function pointer. Change it like this:

 Derived(string (MainClass::*funPtr)(int)) : strFunction(funPtr) {} 

and

 string (MainClass::*strFunction)(int value); 

Remember to call strFunction , you will need an instance of the MainClass object. Often I find it helpful to use typedefs.

 typedef string (MainClass::*func_t)(int); func_t strFunction; 

and

 Derived(func_t funPtr) : strFunction(funPtr) {} 
+8
source share

You can learn more about serious wickedness, which is the C ++ function pointer syntax here .

+5
source share

Yes, the type &MainClass::myFunction is the type of the pointer to the element, while string(*)(int) is the type of the pointer to the function. They are incompatible, since you need to use a link or a pointer to an instance of the class and use operators. * Or → * to use a member-to-member pointer, while a function pointer is not bound to a class and can be called directly.

+4
source share

You are trying to pass a pointer to a member function of the MainClass class, but the function expects a pointer to a regular function, that is, a function that is not a member. Good resume here

The difference is important because member functions have a hidden optional parameter that tells the function for which the "this" pointer applies this function. Thus, pointer types are not interchangeable.

+4
source share

Function pointers (i.e. pointers to unrelated functions) and method pointers (i.e. pointers to non-static functions associated with a class definition) are different in c++ . This is because non-static methods have an implicit this argument, which requires that they always be called in the context of an instance of their class.

You are trying to pass a method pointer to a constructor that takes a pointer to a function. This will not work.

+2
source share

Another syntax issue:

 // assign strFunction to the function pointer passed in Derived(string (*funPtr)(int)) : strFunction(funPtr); 

replace for:

 // assign strFunction to the function pointer passed in Derived(string (*funPtr)(int)) : strFunction(funPtr) {}; 
+2
source share

You might want to examine std :: tr1 :: bind and std :: tr1 :: function , like this (unchecked) code:

 class Derived: public Base { public: typedef std::tr1::function<string(int)> StringFunc; Derived(StringFunc); ... private: StringFunc strFunction; } 

and in the constructor of MainClass:

 myBase = new Derived(std::tr1::bind(&MainClass::myFunction, *this, _1); 

The bind function basically binds a member function to a specific object. Which takes care of this pointer, which is inserted by the compiler.

+1
source share

Object methods have a hidden argument "this". If you pass the method to another class, what will be populated with the "this" argument? Perhaps you can do this with static (cool) methods.

0
source share

As the compiler warning points out, member function pointers are completely different from regular function pointers.

0
source share

All Articles