【问题标题】:Why I cannot use list initializarion for struct with inheritance? [duplicate]为什么我不能将列表初始化用于具有继承的结构? [复制]
【发布时间】:2020-09-25 11:47:24
【问题描述】:

我有一堆从某个接口继承的简单结构,我不能为它们使用列表初始化:

https://godbolt.org/z/PWjPzK

#include <iostream>
#include <string>

class Foo
{
public:
  virtual void write() = 0;
};

struct Bar : public Foo // if I remove this inheritance, it will compile
{
  int num;
  void write()
  {
    std::cout << "num = " << num;
  }
};

int main()
{
    Bar b{ 11 }; // error here
    b.write();
    return 0;
}

编辑:编译器输出带有多个警告和错误:

<source>:21:15: error: no matching function for call to 'Bar::Bar(<brace-enclosed initializer list>)'
   21 |     Bar b{ 11 };
      |               ^
<source>:10:8: note: candidate: 'Bar::Bar()'
   10 | struct Bar : public Foo // if I remove this inheritance, it will compile
      |        ^~~
<source>:10:8: note:   candidate expects 0 arguments, 1 provided
<source>:10:8: note: candidate: 'constexpr Bar::Bar(const Bar&)'
<source>:10:8: note:   no known conversion for argument 1 from 'int' to 'const Bar&'
<source>:10:8: note: candidate: 'constexpr Bar::Bar(Bar&&)'
<source>:10:8: note:   no known conversion for argument 1 from 'int' to 'Bar&&'

【问题讨论】:

  • 请在问题中包含错误信息
  • @Quimby 这不是 POD,因为虚拟方法。
  • @Barmar 我知道,但是错误来自继承,它与虚拟方法无关。

标签: c++ class inheritance constructor list-initialization


【解决方案1】:

结构 Bar 不是聚合,因为它具有虚函数。

来自 C++ 17 标准(11.6.1 聚合)

1 聚合是一个数组或一个类(第 12 条),具有

...

(1.3) — 没有虚函数 (13.3),并且

所以你不能将结构类型的对象初始化为聚合。

另一方面,结构没有带参数的构造函数。

所以编译器对此声明发出错误

Bar b{ 11 };

【讨论】:

  • 那么,我应该为每个结构编写简单的构造函数吗? :(
  • @Sairus 您应该为结构 Bar 编写一个带有参数的构造函数。
猜你喜欢
  • 1970-01-01
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
  • 2023-04-01
  • 2012-02-13
相关资源
最近更新 更多