0

以下代码在g++.

#include <iostream>
#include <string>
using namespace std;

struct thing
{
    int a;
    char b;
    string name;
};

int main()
{
    thing t =
    {
        a : 23,
        b : 'e',
        name : "Hello"
    };

    cout << t.a << endl;
    cout << t.b << endl;
    cout << t.name << endl;

    return 0;
}

我知道{ .a = 23, .b = 'e', .name = "Hello" }C++ 不支持 C99 等效项,但为什么支持上述内容?是否标准化?这个构造函数习语叫什么名字?

从上面的角度来看,如果你的系统有很多用于线程安全消息传递的不可变类(所有constpublic成员),那么使用命名成员构造这样一个类的实例要容易得多,而不是编写一个构造函数并使用位置参数。

4

0 回答 0