Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning C++ function pointers to member functions of the same object

How do I get the function pointer assignments (and maybe the rest) in test.calculate to work?

#include <iostream>

class test {

    int a;
    int b;

    int add (){
        return a + b;
    }

    int multiply (){
        return a*b;
    }

    public:
    int calculate (char operatr, int operand1, int operand2){
        int (*opPtr)() = NULL;

        a = operand1;
        b = operand2;

        if (operatr == '+')
            opPtr = this.*add;
        if (operatr == '*')
            opPtr = this.*multiply;

        return opPtr();
    }
};

int main(){
    test t;
    std::cout << t.calculate ('+', 2, 3);
}
like image 705
toochin Avatar asked Feb 01 '11 15:02

toochin


People also ask

Can we create a pointer to a member function?

You can use pointers to member functions in the same manner as pointers to functions.

Can we declare a function that can return a pointer to a function of the same type?

You cannot return a function in C - you return a pointer to a function. If you mean to define a function which returns a pointer to a function which again returns a pointer to a function and so on, then you can use typedef to implement it.

How do you pass a pointer from one function to another?

Simply pass the argument in func1() to func2() , as a pointer, px in func1() and py in func2() will point to the same memory location, the address of x .

How do you declare a pointer to a member function in C++?

Function pointer to member function in C++ In C++ , function pointers when dealing with member functions of classes or structs, it is invoked using an object pointer or a this call. We can only call members of that class (or derivatives) using a pointer of that type as they are type safe.


2 Answers

There are several problems with your code.

First, int (*opPtr)() = NULL; isn't a pointer to a member function, its a pointer to a free function. Declare a member function pointer like this:

int (test::*opPtr)() = NULL;

Second, you need to specify class scope when taking the address of a member function, like this:

if (operatr == '+') opPtr = &test::add;
if (operatr == '*') opPtr = &test::multiply;

Finally, to call through a member function pointer, there is special syntax:

return (this->*opPtr)();

Here is a complete working example:

#include <iostream>

class test {

    int a;
    int b;

    int add (){
        return a + b;
    }

    int multiply (){
        return a*b;
    }

    public:
    int calculate (char operatr, int operand1, int operand2){
        int (test::*opPtr)() = NULL;

        a = operand1;
        b = operand2;

        if (operatr == '+') opPtr = &test::add;
        if (operatr == '*') opPtr = &test::multiply;

        return (this->*opPtr)();
    }
};

int main(){
    test t;
    std::cout << t.calculate ('+', 2, 3);
}
like image 80
John Dibling Avatar answered Oct 20 '22 14:10

John Dibling


Like this int (test::*opPtr)() = NULL;. Refer http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.1

Edit: Also use if (operatr == '+') opPtr = &test::add; instead of [..] = this.add and return (this->(opPtr))(); instead of return opPtr();. In fact, use typedefs and macros like the FAQ says and probably member function paramaters instead of class members a and b.

like image 41
LumpN Avatar answered Oct 20 '22 15:10

LumpN