具有不同参数的C ++成员函数指针-还是不好吗? [英] C++ member function pointer with different arguments - or is this bad anyway?

查看:68
本文介绍了具有不同参数的C ++成员函数指针-还是不好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使我担心您会告诉我这个话题已经讨论了几次,但我还是敢问这个问题,因为我无法产生解决方案.可能我只是在找错东西...

Even though I fear that you will tell me that this topic was covered several time, I dare to ask it, since I was not able to generate a solution. Probably I was just looking for the wrong thing...

假设我有一个从某些外部函数接收模式"的函数.根据模式,函数将调用同一对象的不同成员函数.对于没有任何参数的成员函数,这对我来说效果很好,但是我没有找到如何将其扩展到具有参数的成员的方法.在实际的应用程序中,参数不是int/float而是一个更复杂的类,并且调用嵌套在不同的循环中,因此我将需要多次放置switch语句,这些操作我觉得很丑.

Assume that I have a function which receives a "mode" from some external function. Depending on the mode, the function will call different member functions of the same object. This works well for me with member function without any argument, but I did not find out how to extend it to members with arguments. In the real world application, the arguments are not int/float but a more complex classes and the call is nested inside different loops, so I would need to put switch statements several times which I consider ugly.

问题A:是否可以根据现有设计轻松添加对带有参数的成员函数的支持?如果是,那该怎么做?如果可能的话,没有外部库...

Question A: Is it possible to easily add support for member functions with arguments based on the existing design? If yes, how does one do that? If possible without external libraries...

问题B:这是完全错误/不好的方法吗?我要如何做得更好?

Question B: Is this a completely wrong/bad approach? How would I do it better?

非常感谢您的帮助和解释.

Thanks a lot for your help and explanations.

克里斯

标题摘录:

typedef void (Object::*memberFunction)();

class Object
{
    void memberFnNoArg();
    void memberFnWithIntArg(int arg);
    void memberFnWithFloatArg(float arg);
}

cpp摘录:

void function()
{
    int mode = getModeFromSomewhere();

    int intArg = 33;
    float floatArg = 66.6;

    switch(mode)
    {
    case 1:
        process(&Object::memberFnNoArg);
        break;
    case 2:
        process(&Object::memberFnWithIntArg, ???); // how can I pass arg?
        break;
    case 3:
        process(&Object::memberFnWithFlaotArg, ???); // how can I pass arg?
        break;
    default:
        // do nothing;
    }

}

void process(Object::memberFunction func)
{
    Object object;
    // loops, called several times, ...
    (object.*func)(); // how do I handle different arguments?
}

推荐答案

在函子中包装算法是正确的方法,而std::function是标准库提供的很好的函子.

Wrapping the algorithm in a functor is the right approach, and std::function is a nice functor provided by the Standard library.

但是,根据Tomek的建议,使用boost::bind甚至std::bind确实是丑陋的IMO,并且在绑定多个参数时会很快失去控制.

But using boost::bind or even std::bind, as suggested by Tomek, is really ugly IMO, and rapidly gets out of control when binding multiple arguments.

如果您使用的是最新的编译器,则可以使用lambda代替,这使Tomek的示例看起来像:

If you have a recent compiler you can use a lambda instead, which makes Tomek's example look like:

std::function<void(Object*)> f  =
    [](Object* const that){ that->memberFnNoArg(); };

int int_value = 22;
std::function<void(Object*)> f2 =
    [int_value](Object* const that){ that->memberFnIntArg(int_value); };

Object o;
f(&o);
f2(&o);

有几个字符可以设置lambda,但是成员访问语法非常自然,很明显如何进行更改.

There are a few characters to set up the lambda, but the member access syntax is extremely natural and it's obvious how you make changes.

当然,如果确实需要,可以使参数成为对对象的引用,但是我更喜欢这里的指针.

Of course, you can make the parameter a reference to the object if you really want, but I prefer pointers here.

这篇关于具有不同参数的C ++成员函数指针-还是不好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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