聚合类的列表初始化

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-30 13:06   11   0

在C++11中,聚合类可以列表初始化,一个聚合类的条件有:

  • 所有成员都是public
  • 没有定义构造函数
  • 没有类内初始值
  • 没有基类,也没有虚函数

其中“没有类内初始值”这条,在C++14中已经废止, 如:

struct Person{
 std::string name;
 int height = 0;
};

int main(){
 Person person={"xiaohong",10};
 std::cout << person.name<<" "<<person.height << std::endl;
 system("pause");
 return 0;
}

以上代码用 g++ ... -std=c++11编译报错, 用g++ ... -std=c++14就没问题。

https://stackoverflow.com/questions/18184096/c11-struct-initialization-compilation-error

https://blog.csdn.net/xiongya8888/article/details/89290184

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP