标签:
#include <iostream>using namespace std;class Base1{public:Base1() = default;Base1(const string & str){strValue = str;}string strValue;};class Base2{public:Base2() = default;Base2(const string & str){strVal2nd = str;}string strVal2nd;};/*** 一般情况下子类并不继承父类的构造函数* 仅仅是在初始化父类的成员变量时调用父类的构造函数* C++11允许通过下述语句来继承父类的构造函数* using ParentClass::ParentClass;* 这时需要注意,不要因为继承不同基类的构造函数而引起二义性*/class D: public Base1, public Base2{public:using Base1::Base1;// 从Base1继承构造函数using Base2::Base2;// 从Base2继承构造函数/*** 如果同时继承了上述两个基类的构造函数* 则D必须实现自己的构造函数从而覆盖继承的构造函数*/D() = default;D(const string & str)// 这个构造函数不能少{D(str, str);}D(const string & str1, const string & str2): Base1(str1), Base2(str2){}};int main(){D d("Hello", "World");cout << d.strValue << "\t" << d.strVal2nd << endl;return 0;}
标签:
原文地址:http://www.cnblogs.com/fengkang1008/p/4652216.html