【问题标题】:C++ std::vector assign methodsC++ std::vector 赋值方法
【发布时间】:2018-06-28 16:59:06
【问题描述】:

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

#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;
}

【问题讨论】:

  • 为什么要拥有它们?可能的 XY 问题。
  • 见this post 了解一些想法。

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


【解决方案1】:

看来你想要std::vector&lt;std::function&lt;void()&gt;&gt;:

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

Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-05
    • 2012-02-01
    • 1970-01-01
    • 2017-08-22
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    相关资源
    最近更新 更多