设置指向非静态成员函数的指针 [英] Setting a pointer to a non-static member function

查看:64
本文介绍了设置指向非静态成员函数的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置一个函数指针,该函数指针是在执行期间根据一组用户参数设置的.我想让函数指针指向一个非静态成员函数,但是我找不到怎么做.

I'm trying to setup a function pointer that is set during execution based on a set of user parameters. I would like to have the function pointer point to a non-static member function but I can't find how to do it.

我所看到的示例说,这只能通过静态成员函数来完成,或者只能在直的C语言中使用全局变量.

The examples I've seen say this can only be done with static member function only or use global variables in straight C.

一个简化的示例如下:

    class CA
    {
    public:
        CA(void) {};
        ~CA(void) {};
        void setA(double x) {a = x; };
        void setB(double x) {b = x; };

        double getA(const double x) {return x*a; };
        double getB(const double x) {return x*b; };

        void print(double f(const double), double x) {
            char cTemp[256];
            sprintf_s(cTemp, "Value = %f", f(x));
            std::cout << cTemp;
        };

    private:
        double a, b;
    };

实现部分是

CA cA;
cA.setA(1.0);
cA.setB(2.0);

double (*p)(const double);

if(true) {
    p = &cA.getA;  //'&' : illegal operation on bound member function expression
} else {
    p = cA.getB;  //'CA::getB': function call missing argument list; use '&CA::getB' to create a pointer to member
                  //'=' : cannot convert from 'double (__thiscall CA::* )(const double)' to 'double (__cdecl *)(const double)'
}

cA.print(p, 3.0);

所以我如何使p指向'getA'或'getB',以便它仍可被'print'使用.

So how do I get p to point to either 'getA' or 'getB' so that it is still useable by 'print'.

从我所看到的,建议是使用boost或std :: bind,但是我对这两个都没有经验.我希望我不需要深入研究这些东西,而只是想念一些东西.

From what I have seen, the suggestions are to use boost or std::bind but I've had no experience with either of these. I'm hoping that I don't need to dive into these and that I'm just missing something.

编译器MSVC ++ 2008

Compiler MSVC++ 2008

推荐答案

不要忘记成员函数接受隐式的 this 参数:因此,成员函数接受 double 不能与接受 double 的非成员(免费)函数相同.

Don't forget that a member function accepts an implicit this parameter: therefore, a member function accepting a double can't be the same thing as a non-member (free) function accepting a double.

// OK for global functions
double (*p)(const double);

// OK for member functions
double (CA:*p)(const double);

调用的方式也不同.首先,对于成员函数,需要一个对象来对其进行调用(其地址最终将绑定到函数调用中的 this 指针).其次,您需要使用.* 运算符(如果正在通过指针执行调用,则需要使用-> * 运算符):

Also the way you invoke them is different. First of all, with member functions, you need an object to invoke them on (its address will eventually be bound to the this pointer in the function call). Second, you need to use the .* operator (or the ->* operator if you are performing the call through a pointer):

p = &CA::getA;
CA cA;
(cA.*p)();

始终,您将必须更改函数 print()的定义:

Consistently, you will have to change your definition of function print():

    #include <iostream>

    void print(double (CA::*f)(const double), double x) 
    {
        // Rather use the C++ I/O Library if you can...
        std::cout << "Value = " << (this->*f)(x);
    };

所以最后,这是您应该重写 main()函数的方法:

So finally, this is how you should rewrite your main() function:

int main()
{
    CA cA;
    cA.setA(1.0);
    cA.setB(2.0);

    double (CA::*p)(const double);

    if (true) // Maybe use some more exciting condition :-)
    {
        p = &CA::getA;
    } 
    else {
        p = &CA::getB;
    }

    cA.print(p, 3.0);
}

这篇关于设置指向非静态成员函数的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆