C++11构造函数继承不起作用

C++11 constructor inheritance not working

本文关键字:不起作用 继承 构造函数 C++11      更新时间:2023-10-16

我尝试了以下代码:

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);进行编译?

根据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即将发布的版本将对此提供支持。