成员聚合初始化列表不受支持?

时间:2011-08-25 16:38:09

标签: c++ visual-studio-2008

struct bar { int x; };

struct qux
{
    static const int foo[3] = { 1, 2, 3 }; // Error

    static const bar baz = { 0 }; // Error
};

Visual Studio 2008;标准;

syntax error : '{'

1 个答案:

答案 0 :(得分:4)

只能在类中初始化整数 static 非聚合成员。所有其他静态成员(聚合或其他)必须在类外部初始化。

//tag.h

struct tag
{
    static const int foo[3];  //aggregate
    static const bar baz;    //aggregate
    static const std::string s; //non-aggregate (non-integral type)
    static const int x = 10; //ok : non-aggregate (integral type)
};

//tag.cpp

const int tag::foo[3] = { 1, 2, 3 }; //ok
const bar tag::baz = { 0 }; //ok
const std::string s = "example"; //ok
const int tag::x; //definition - if you want to take its address