【问题标题】:Preserve default constructor in derived classes在派生类中保留默认构造函数
【发布时间】:2018-05-12 19:35:43
【问题描述】:

如何在派生类中保留默认构造函数,而不在派生类中添加Derived() = default;

struct Base
{
  Base() = default;
};

struct Derived : Base
{
  using Base::Base;

  explicit Derived(int) {}
};

int main()
{
  // Compilation error here
  Derived d;
}

【问题讨论】:

  • 无法复制。你遇到了什么错误?
  • 有什么理由不想显式默认默认构造函数?
  • @NicolBolas 不是真的,但由于我有很多派生类,我想避免很多明确的默认 c'tors..
  • 使用 GCC 7.2.0 为我编译。

标签: c++ c++14 c++17


【解决方案1】:

看起来像一个错误。它适用于更高版本的 gcc。我试过7.1,它工作。也与clang一起工作。你用的是哪个编译器?

见:

Constructor inheritance for class derived from template class in visual studio 2015 rc

【讨论】:

    猜你喜欢
    • 2015-10-30
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2012-12-10
    • 1970-01-01
    • 2012-11-24
    相关资源
    最近更新 更多