Apply functions belonging to other classes through variadic templates

前端 未结 1 1679
眼角桃花
眼角桃花 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条回答
  • 2021-01-23 14:34

    From your current interface, you might do something like:

    class Controller
    {
     public:
        template <class...Classes>
        Controller(Classes&... classes)
        {
            mF = [&](int val) { (classes.updateValue(val), ...); }; // Fold expression from C++17
        }
    
        void setValues(int val)
        {
            mF(val);
        }
        private:
            std::function<void(int)> mF;
        };
    
    0 讨论(0)
提交回复
热议问题