本文介绍了为什么拥有基类会取消类的聚集资格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 拥有一个聚合的公共基类(甚至多个聚合的公共基类)会导致一个类失去聚合类的良好属性,这是什么?What is it about having an aggregate public base class (or even multiple aggregate public base classes) that would make a class lose the nice properties of aggregate classes?来自的聚合基类的定义 http://en.cppreference.com / w / cpp / language / aggregate_initialization http:// zh_CN.wikipedia.org/wiki/C++_classes#Aggregate_classes 聚合类的漂亮属性: 无需定义构造函数,可以通过将大括号括起来的值列表传入来初始化其成员(或基本类,如果它们允许的话。) 聚合类型被认为是简单的( PODs ),并且可以用作 constexpr s 。 Without defining a constructor, an aggregate type can be initialized by passing in a brace-enclosed list of values to initialize its members (or base classes, if they had allowed them).Aggregate types are considered "simple" (a generalization of PODs), and can be used as a literal type for the purposes of constexprs. http://en.cppreference.com/w/cpp/language/aggregate_initialization#Example :#include <string>#include <array>struct S { int x; struct Foo { int i; int j; int a[3]; } b;};int main(){ S s1 = { 1, { 2, 3, {4, 5, 6} } }; S s2 = { 1, 2, 3, 4, 5, 6}; // same, but with brace elision}另请参见: 什么是聚合和POD,以及它们如何/为什么 推荐答案根据 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3308.pdf 尽管标准有明确的意图,但类型B不是文字类型: Despite the clear intentions of the standard, type B is not a literal type:struct A {}; struct B : A {};这是因为直到被odr使用之前,它的构造函数才隐式定义。没有constexpr构造函数。This is because its constructor is not implicitly defined until it is odr-used, and until that point it has no constexpr constructors.不过,老实说,不确定这是什么意思。Honestly not sure what this means, though. 这篇关于为什么拥有基类会取消类的聚集资格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-26 10:11