当一个结构有c-tor时,为什么我不能静态初始化它呢?

Sol*_*Out 3 c++ static constructor structure initialization

我的问题:当一个结构有c-tor时,为什么我不能静态初始化它?

我的编译器声称:

type `myStruct' must be initialized by constructor, not by `{...}'
Run Code Online (Sandbox Code Playgroud)

这是为什么 ?我正在使用gcc版本3.4.4(cygming special,gdc 0.12,使用dmd 0.125)

为了说明,这是struct编译器拒绝的.

struct myStruct
{
    int a;
    double b;

    myStruct() { a= 0; b = 0.0; }
}

void main()
{
    myStruct ms = {7, 7.7}; // Now this compiler does not accept.
} 
Run Code Online (Sandbox Code Playgroud)

Jas*_*son 7

包含用户定义的c-tor意味着它不再是聚合类型.如果没有自己的用户定义的c-tor,也会出现这种情况struct,但是你的非静态数据成员struct不是POD或聚合类型.