c++ - 继承构造函数不允许类似数组的初始化

标签 c++ inheritance c++11 constructor

这是 g++ 错误吗?

#include <array>

struct inherit : std::array<int , 3>{
    using std::array<int , 3>::array;
};

std::array<int, 3> ok1 = {1,2,3};
inherit ok2;
inherit bad = {1,2,3};

实例化 bad , 我得到 error: could not convert ‘{1, 2, 3}’ from ‘<brace-enclosed initializer list>’ to ‘inherit’ .在我看来它绝对正确。

最佳答案

std::array 是一个集合。它没有用户定义的构造函数。

来自 C++ 标准

1 An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).

类继承不是聚合,因为它有一个基类。所以它可能不使用大括号初始化(除了一个空的初始化列表)。如果你想用一个非空的初始化列表初始化它,你应该显式地为类继承定义构造函数,

关于c++ - 继承构造函数不允许类似数组的初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22161773/

相关文章:

c++ - 重绘时如何避免重新启动多边形?

c++ - 在 C++ 中使用文件时出现段错误

c++ - Stack 上的所有值是否都小于传递值?

c# - 通用类型约束不适用

c# - 派生类中引入的公共(public)接口(interface)

c++ - 模板成员函数上的外线 sfinae 是否可能?

c++ - 就地数组更改

c++ - 用于编译原始二进制文件的 Windows 的 C 编译器?

c++ - 为什么 QIterator 对象对于放置在两个不同位置的同一个文件夹表现不同?

python - 使用superclass .__ init()__而不是superclass()调用父类(super class)构造函数的原因