简体   繁体   中英

Initializing a class' function pointer member to the same class' function member

In the header file of class Class1 , I've declared a function pointer fn_ptr and a function called myfunc(int,int) :

class Class1
{
   private: 
      bool (*fn_ptr)(int,int); 

   public:     
      Class1(); //Default Constructor
      bool myfunc(int,int); 
};

I want to have fn_ptr point to Class1 's myfunction defined like so in the class's library file:

Class1::Class1()
{
   fn_ptr = myfunc; //Initialization in the 
}

Class1::myfunc(int a, int b)
{
   return a<b; 
}

Compiling resulted in an error:
error: argument of type 'bool (Class1::)(int,int)' does not match 'bool (*)(int,int)'

As user4581301 wrote in the comment, once you have a (non- static ) member function, it takes a "hidden" pointer to the class's object, so you can't do directly what you tried here - the interfaces just don't match.

Also, there are currently more convenient facilities for function indirection that bare pointers.

Suppose you do the following. First, declare the class like this:

#include <functional>                                                                                                                                                                                    



using namespace std;


class Class1
{
private:
    std::function<bool(int, int)> fn;

public:     
    Class1(); //Default Constructor
    bool myfunc(int,int){ return true; }
};

Note how the member fn is of type std::function , which is more convenient and versatile.

Now in the constructor, you can simply initialize it with a lambda function :

Class1::Class1() :
    fn([this](int i, int j){return myfunc(i, j);})
{

}

It says that for any i and j , just call myfunc (on this objet) on these arguments, and return the result.


Full code

Make sure to build using c++11 settings.

#include <functional>                                                                                                                                                                                    



using namespace std;


class Class1
{
private:
    std::function<bool(int, int)> fn;

public:     
    Class1(); //Default Constructor
    bool myfunc(int,int){ return true; }
};


Class1::Class1() :
    fn([this](int i, int j){return myfunc(i, j);})
{

}

int main()
{
    Class1 c;
}

In addition to @Ami's excellent answer about using std::function instead, member function pointers have different syntax, which is why it didn't work for you.

bool (Class1::*fn_ptr)(int,int); //note the Class1::

fn_ptr = &Class1::my_func; //the &Class1:: is necessary, forgetting it is a compilation error

Calling it is different, too. It needs an instance of the class to be called on.

Class1 c;
(c.*fn_ptr)(1, 2); //or (this->*fn_ptr) too

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM