Apply functions belonging to other classes through variadic templates

前端 未结 1 1680
眼角桃花
眼角桃花 2021-01-23 13:51

Say I have three classes, ClassA, ClassB, and ClassC. And all three of these classes have a function called updateValue(int).

1条回答
  •  梦毁少年i
    2021-01-23 14:34

    From your current interface, you might do something like:

    class Controller
    {
     public:
        template 
        Controller(Classes&... classes)
        {
            mF = [&](int val) { (classes.updateValue(val), ...); }; // Fold expression from C++17
        }
    
        void setValues(int val)
        {
            mF(val);
        }
        private:
            std::function mF;
        };
    

    0 讨论(0)
提交回复
热议问题