Kunal Shrivastava
Kunal Shrivastava

Reputation: 612

how to use functor and function pointer together? unable to assign function to a function pointer

I am just trying out c++11. I want to demonstrate functor and function pointer in the same program but I keep getting an error.how to use functor and function pointer together? unable to assign function to a function pointer. unable to assign the function test1 and test2 to function pointer foo cannot assign Functortest* to a void* is the error i am getting. what should i do, why am i not able to assign the function to the function pointer foo and foo1

 #include<iostream>
 #include<string>
 using namespace std;



 class Functortest{

 public:
     Functortest(){
         //foo=void(*)();

         //void(*foo1)(string);
     }
     void operator()(int option){
         switch (option){
         case 1: 
             void(*foo)();
             foo=test1;
             break;
         case 2:
             void(*foo1)();
             foo = test2;
         }

     };
     void test1(){

         cout << "TEST1 CALLED";
     }
     void test2(string msg){

         cout << "TEST2 CALLED msg:  "<< msg;
     }
 private:
     void *foo;
     void *foo1;
     /*void(*foo)();
     void(*foo1)(string);*/
 };

 void main(){

     Functortest funct;
     funct(1);
     funct(2);
 }

Upvotes: 0

Views: 89

Answers (1)

dyp
dyp

Reputation: 39151

The parashift C++ FAQ contains quite some information on how you can use member function pointers.

#include<iostream>
#include<string>
//using namespace std; // <-- better avoid `using namespace` at file scope


class Functortest {
public:
    Functortest()
        : foo(&Functortest::test1)   // better use the mem-initializer-list
        , foo1(&Functortest::test2)  // to initialize members
    {}

    void operator()(int option){
        switch (option){
        case 1: 
            (this->*foo)();
            foo = &Functortest::test1;    // after the call?
            break;
        case 2:
            (this->*foo1)("Hello World"); // need to pass an argument
            //foo = &Functortest::test2;  // <-- this won't work! (A)
            break;                        // better always end with a break
        }
    };

    void test1() {
         std::cout << "TEST1 CALLED\n";
    }
    void test2(std::string msg) {
         std::cout << "TEST2 CALLED msg: " << msg << "\n";
    }

private:
    void (Functortest::*foo)();             // declares a data member foo
    void (Functortest::*foo1)(std::string); // declares a data member foo1
};

int main() {             // NOT `void main`
    Functortest funct;
    funct(1);
    funct(2);
}

On the line commented with // <-- this won't work! (A):

As you can see from the declarations of the data members foo and foo1, those two have different types:

  • foo is of the type void (Functortest::*)(), i.e. foo is a pointer to a member function of class Functortest, which takes no arguments and returns nothing. It is the same type as a pointer to the specific member function test1 of class Functortest.

  • foo1 is of the type void (Functortest::*)(std::string), i.e. foo1 is a pointer to a member function of class Functortest, which has a parameter of type std::string and returns nothing. It is the same type as a pointer to the specific member function test2 of class Functortest.

A function that takes no arguments cannot be called with an argument (trivial), similarly, a function that has a parameter cannot be called with no arguments. Therefore, the types of foo and foo1 are incompatible. For the same reason, you cannot assign a pointer to Functortest::test1 to a pointer to a member function [...] which takes an argument of type std::string.

Upvotes: 3

Related Questions