为什么在成员初始值设定项列表中初始化数组时不需要括号?

时间:2021-01-05 08:46:42

标签: c++ arrays class initialization class-constructors

例如,我有:

#include <iostream>

class Foo {
private:
    int size;
    int arr[10];
public:
    Foo();
};

Foo::Foo()
    : size {0}, arr { } {
    
}

int main() {

    Foo one;

}

这有效,但如果我尝试使用括号:

#include <iostream>

class Foo {
private:
    int size;
    int arr[10];
public:
    Foo();
};

Foo::Foo()
    : size {0}, arr[] { } { // error
    
}

int main() {

    Foo one;

}

为什么在成员初始值设定项列表中初始化数组时不需要括号?这只是语法吗?这种行为是否在其他任何地方都可以找到初始化数组?

0 个答案:

没有答案
相关问题