Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对子类使用父类构造函数_C++_Visual Studio_C++11_Constructor_Polymorphism - Fatal编程技术网

C++ 对子类使用父类构造函数

C++ 对子类使用父类构造函数,c++,visual-studio,c++11,constructor,polymorphism,C++,Visual Studio,C++11,Constructor,Polymorphism,所以,我想要一个类Child,它拥有来自其父类的所有构造函数。这在C++中可能吗?我尝试过使用语句,但它不起作用。以下是到目前为止我得到的信息: struct Base{ Base(int i){ std::cout << "Construcetd a base with " << i << std::endl; } }; struct Child : public Base{ using Base::Base;

所以,我想要一个类Child,它拥有来自其父类的所有构造函数。这在C++中可能吗?我尝试过使用语句,但它不起作用。以下是到目前为止我得到的信息:

struct Base{
    Base(int i){
        std::cout << "Construcetd a base with " << i << std::endl;
    }


};


struct Child : public Base{
    using Base::Base;

};




int main(){

    Child c(1);
}
哦,我正在使用Visual Studio 2013,正如您所看到的,继承构造函数是Microsoft Visual Studio尚不支持的C++11功能


您发布的代码是正确的,与C++11编译器预期的一样

编译器尝试调用默认的
子构造函数(无参数)。因此,要使代码正常工作,您需要提供显式构造函数:

struct Child : public Base{
    using Base::Base;
    Child(int i):Base(i) {}

};

继承构造函数是C++11语言的特性

struct A { A(int); };
struct B: A { using A::A; }; // defines B::B(int)
B b(42); // OK

但是,Visual Studio 2013不支持它,如中所述。

为此您需要C++11支持。您的编译器似乎不支持此功能。您没有抓住要点。OP希望他们的C++代码完全有效,但他们的编译器不够现代。
struct A { A(int); };
struct B: A { using A::A; }; // defines B::B(int)
B b(42); // OK