函数指针分配给同一对象的成员函数

函数指针分配给同一对象的成员函数

本文介绍了将C ++函数指针分配给同一对象的成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让test.calculate中的函数指针赋值(也许是其余的)工作?

  #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);
}


解决方案


首先, int(* opPtr)()= NULL; 不是指向成员的指针函数,它的一个指向自由函数的指针。声明一个成员函数指针如下:



int(test :: * opPtr)()= NULL; / p>

其次,在采取成员函数的地址时,您需要指定类作用域,例如:

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

最后,要通过成员函数指针调用,还有一些特殊的语法:

  return(this-> * opPtr)(); 

这是一个完整的工作示例:

  #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);
}


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);
}
解决方案

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);
}

这篇关于将C ++函数指针分配给同一对象的成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 05:06