相关疑难解决方法(0)

C++/C++ 11使用初始化列表初始化对象的静态数组/向量的高效方法,并支持基于范围的

假设您想要一个与类关联的预定义值/对象(const或非const)的静态数组.可能的选项是使用std:vector,std::arrayC-style array (ie. []),或.例如,

在.hpp:

class MyClass {
public:
    static const std::vector<MyClass> vec_pre; // No efficient way to construct with initializer list, since it always uses Copy Contructor, even when using std::move
    static const std::array<MyClass, 2> arr_pre; // Have to specify size which is inconvenient
    static const MyClass carr_pre[]; // Not compatible with C++11 for-range since size is undefined
};
Run Code Online (Sandbox Code Playgroud)

在.cpp

const std::vector<MyClass> MyClass::vec_pre = { std::move(MyClass{1,2,3}), std::move(MyClass{4,5,6})  }; // NOTE: This still uses …
Run Code Online (Sandbox Code Playgroud)

c++ static for-loop c++11

14
推荐指数
1
解决办法
3633
查看次数

标签 统计

c++ ×1

c++11 ×1

for-loop ×1

static ×1