C ++ std :: vector分配方法

时间:2018-06-28 16:59:06

标签: c++ pointers vector c++17

有人知道如何通过工作代码实现这一目标吗? 我的愿望是用类的可执行方法填充向量...

#include <iostream>
#include <vector>
#include <any>

class foo {
    public:
        void boo() {
            std::cout << "WM 2018" << std::endl;
        }
};

int main(int argc, char const *argv[])
{
    std::vector<std::any> vec;
    vec.push_back( (new foo)->boo() );

    vec[0]();

    return 0;
}

1 个答案:

答案 0 :(得分:1)

似乎您想要std::vector<std::function<void()>>

std::vector<std::function<void()>> vec{[](){ foo{}.boo();}};

Demo

相关问题