c++ - lambda 作为实例函数指针

标签 c++ lambda function-pointers

我的类型 aButton 有一个函数指针,所以我可以为每个按钮定义自定义操作,尽管最简单的方法是创建一个 lambda 并取消引用它并将它传递给该 aButton 实例的函数指针,因为我需要对按钮类范围之外的对象进行非静态访问

但我一直在试图弄清楚如何将它转换为正确的类型以及如何在不出现以下错误的情况下调用它......我没有看到很多人幸运地做到这一点,并且使用函数式不会'看起来我不能传递上下文???

    // Example program
    #include <iostream>
    #include <string>

    int global1 = 0;
    int global2 = 5;

    class aButton {
    public:
        int status, oldStatus;
        aButton(int initStatus) { oldStatus = status = initStatus; }
        int (aButton::*action)();
    };

    class Thingy {
    private:
        int mode  = 1;
        int value = 0;
    public:
        void reset() { value = 0; }
        void setMode(int newMode) { mode = newMode; }
        void increment() { value = value + global2; }
        //accessors & mutators
    };

    void specialFunction(Thingy *thingyToWorkOn) {
        //do stuff...
    }
    void anotherSpecialFunction(Thingy *firstThingy, Thingy *secondThingy) {
        //more stuff...
    }

    int main() {

        Thingy one;
        Thingy two;

        aButton *on = new aButton(0);
        aButton *speedUp = new aButton(0);

        on->action = &(     //error: taking address of temporary [-fpermissive]
            [&]() {         //error: cannot convert 'main()::<lambda()>*' to 'int (aButton::*)()' in assignment

                //some specific stuff....
                global1 = 1;
                if (global2 < 10) {
                    global2++;
                }
                one.reset();
                two.reset();
                anotherSpecialFunction(&one, &two);
                std::cout << "on action \n";
                return 1;
            }
        );

        speedUp->action = &(    //error: taking address of temporary [-fpermissive]
            [&]() {             //error: cannot convert 'main()::<lambda()>*' to 'int (aButton::*)()' in assignment

                //some specific stuff....
                if (global1) {
                    one.setMode(global2);
                    two.setMode(global2);
                    specialFunction(&one);
                    specialFunction(&two);
                    std::cout << "speedUp action \n";
                    return 1;
                }
                return 0;
            }
        );


        for(int i=0; i<5; i++) {
            //if on pushed
            (on->(on->action))();           //error: expected unqualified-id before '(

            //if speedUp pushed
            (speedUp->(speedUp->action))(); //error: expected unqualified-id before '(
        }

    }

最佳答案

我相信你想要aButton::action类型为 std::function<int()> (阅读:不接受任何东西并返回 int 的函数)而不是 int (aButton::*) .这需要 <functional> header 。通过这种更改,您的分配可以保持不变(减去前导地址运算符),但正如您所想的那样,您需要使用 -> int 显式声明返回类型。 .调用将简单地采用以下形式(例如):

on->action();

另一个注意事项:通过引用捕获局部变量(onetwo)时要非常小心。如果函数真的是 main那我想没关系,因为main直到程序结束才会返回,否则就是自讨苦吃。

关于c++ - lambda 作为实例函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25327354/

相关文章:

c# - 使用 "=>"的语法说明

java - 在 JUnit 测试中,有没有一种方法可以确保所有断言都已执行?

java - 如何使用方法引用调用参数化方法

c - 是否可以交换 C 函数?

C:使用带有函数指针的二维数组

C++ 通过指针传递/访问矩阵

c++ - 最佳实践 : how to interpret/process QDataStream?

c++ - 通过函数指针调用函数时出错(错误C2064)

c++ - 为什么这个构造函数重载不起作用? (发生模板和类型定义)

c++ - __builtin_ctz(0)或__builtin_clz(0)有多不确定?