相关疑难解决方法(0)

在C++中的类初始值设定项中初始化const数组

我在C++中有以下类:

class a {
    const int b[2];
    // other stuff follows

    // and here's the constructor
    a(void);
}
Run Code Online (Sandbox Code Playgroud)

问题是,如何在初始化列表中初始化b,因为我无法在构造函数体内初始化它,因为b是const

这不起作用:

a::a(void) : 
    b([2,3])
{
     // other initialization stuff
}
Run Code Online (Sandbox Code Playgroud)

编辑:这个例子就是我可以b为不同的实例设置不同的值,但是已知这些值在实例的生命周期内是不变的.

c++ array-initialize initialization c++03

73
推荐指数
5
解决办法
12万
查看次数

标签 统计

array-initialize ×1

c++ ×1

c++03 ×1

initialization ×1