【问题标题】:C++11 constructor inheritance not workingC ++ 11构造函数继承不起作用
【发布时间】:2014-10-19 16:13:36
【问题描述】:

我尝试了以下代码:

struct Base {
    Base(int a) {}
};

struct Derived : Base {
    using Base::Base; // Inherit Base's constructors.
};

int _tmain(int argc, _TCHAR* argv[])
{
    Derived d(1);
}

这不编译,我得到:

error C2664: 'Derived::Derived(const Derived &)' : cannot convert argument 1 from 'int' to 'const Derived &' 

(找不到相关的构造函数,尝试调用默认的复制构造函数)

如何修复继承以便Derived d(1); 能够编译?

【问题讨论】:

  • 这可能取决于编译器,也许您的编译器未配置为 C++11 模式?请提供有关您的编译器的详细信息。
  • 上面的代码用g++ 4.8.2和clang++ 3.5编译。你的编译器是什么?
  • 适用于 gcc4.9。我猜这是一个 MSVC 错误。
  • Visual Studio 2013 (v120)
  • 绝对是编译器的bug

标签: c++ inheritance c++11 constructor


【解决方案1】:

根据http://msdn.microsoft.com/en-us/library/hh567368.aspx 构造函数继承在任何现有的Visual C++ 编译器中都没有实现:(

但是,如果我们有耐心再等一段时间,根据http://blogs.msdn.com/b/vcblog/archive/2014/06/11/c-11-14-feature-tables-for-visual-studio-14-ctp1.aspx 的说法,即将发布的版本将对此提供支持。

【讨论】:

    猜你喜欢
    • 2013-05-21
    • 2013-12-24
    • 1970-01-01
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 2023-03-27
    • 2013-09-04
    相关资源
    最近更新 更多