还是不太理解继承中的构造函数 再问一下
class Base
{
public:
Base(int i)
{}
};
class Dervice:public Base
{
public:
Dervice():Base(10){}//这个地方 是子类的初始化列表 这里写"Base(10)" 不就是对基类构造函数的调用吗?既然是对基类构造函数的调用 是否就说明了子类中继承了基类的构造函数呢?
};
[解决办法]
#include <iostream>using namespace std;class base{ public: base(int i):num(i){} base(const base &orig):num(orig.num){} base& operator = (const base &orig) { num = orig.num; return *this; } private: int num;};class derived:public base{ public: derived(int a,int b):base(a),id(b) { //base::base(a);//或者换成base(a);<<---- base::base(a);测试是否被继承并应藏,base(a);测试是否被继承没有隐藏,结果全部编译错误 //id = b; } derived(const derived &orig):base(orig),id(orig.id) { //base::base(orig);//或者换成base(orig);<<-----同上,结果全部编译错误 //id = orig.id; } derived& operator = (const derived &orig) { base::operator = (orig);//<<--------通过,说明这个被继承但被隐藏 id = orig.id; return *this; } private: int id;};int main(){ return 0;}
[解决办法]