多层继承中常见问题

目录

一、派生类的构造函数问题

1.派生类构造函数不明确指出调用基类构造函数

 二、派生对象的拷贝构造问题

1.基类和派生类都无自定义拷贝构造函数

 2.基类有自定义拷贝构造函数,而派生类没有

3.派生类有自定义拷贝构造函数,基类没有

4. 派生类有自定义拷贝构造函数,基类也有

三、派生对象的拷贝赋值函数问题 

1.都没有拷贝赋值函数

2.基类有,派生类没有

 3.派生类有,基类没有

4.都有拷贝赋值函数


一、派生类的构造函数问题

1.派生类构造函数不明确指出调用基类构造函数

如:

class Int
{
private:
    int value;
public:

    Int(int x = 1) : value{ 1 } {
        cout << "Create Int : " << this << endl;
    }
    ~Int() 
    { cout << "Destroy Int: " << this << endl; 
    }
    Int(const Int& it) :value(it.value) {
        cout << "Copy Create Int " << this << endl;
    }  
    void Print() const {
        cout <<"Int:: "<< value << endl;
    }
};
class Object: public Int
{
private:
    int xval;
public:
    Object(int x = 0) :xval(x + 10) {  //未显示调用基类构造函数
        cout << "Creat Object " << endl;
    }
    ~Object()
    {
        cout << "Destory Object " << endl;
    }
    void Print() const
    {
        Int::Print();
        cout << "Object:: " << xval << endl;
    }
};
int main()
{
    
    Object obj;
    obj.Print();
}

此时,创建派生类对象时,编译器会自动调用基类的缺省构造函数,进行构建基类对象。

 

基类Int中的value 为默认值1;

当基类无缺省构造函数时,此时编译器无法给出匹配的构造函数,会报错,无法编译通过;

 二、派生对象的拷贝构造问题

1.基类和派生类都无自定义拷贝构造函数

如:

class Int
{
private:
    int value;
public:

    Int(int x = 2) : value{ x } {
        cout << "Create Int : " << this << endl;
    }
    ~Int() 
    { cout << "Destroy Int: " << this << endl; 
    }
    void Print() const {
        cout <<"Int:: "<< value << endl;
    }
};
class Object: public Int
{
private:
    int xval;
public:
    Object(int x = 0) : Int(x+1) , xval(x + 10) {
        cout << "Creat Object " << endl;
    }
    ~Object()
    {
        cout << "Destory Object " << endl;
    }
    void Print() const
    {
        Int::Print();
        cout << "Object:: " << xval << endl;
    }
};
int main()
{
    
    Object obj;
    Object s1(obj);
    obj.Print();
    s1.Print();

}

当类中无自定义的拷贝构造函数时,系统会给出默认的缺省构造函数(按位复制) 

进行  Object s1(obj);时,系统会自动调用基类和派生类的默认缺省函数,所以可以正常拷贝赋值。

 2.基类有自定义拷贝构造函数,而派生类没有

如:

class Int
{
private:
    int value;
public:

    Int(int x = 1) : value{ x } {    }
    ~Int()     {     }
    Int(const Int& it) :value(it.value) {
        cout << "Copy Create Int " << this << endl;
    }
    void Print() const {
        cout <<"Int:: "<< value << endl;
    }
};
class Object: public Int
{
private:
    int xval;
public:
    Object(int x = 0) :Int(x),xval(x + 10) {    }
    ~Object()    {    }
    void Print() const
    {
        Int::Print();
        cout << "Object:: " << xval << endl;
    }
};
int main()
{
    
    Object obj;
    Object s1(obj);
    obj.Print();
    s1.Print();
}

进行  Object s1(obj);操作,系统会调用派生类的默认缺省拷贝构造函数,并且调用基类自定义拷贝构造函数,可以成功进行拷贝。

3.派生类有自定义拷贝构造函数,基类没有

如:

 

class Int
{
private:
    int value;
public:

    Int(int x = 1) : value{ x } {    }
    ~Int()     {     }
    void Print() const {
        cout <<"Int:: "<< value << endl;
    }
};
class Object: public Int
{
private:
    int xval;
public:
    Object(int x = 0) :Int(x),xval(x + 10) {    }
    ~Object()    {    }
    Object(const Object& s): xval { s.xval }
    {
        cout << "Copy Create Object" << this << endl;
    }
    void Print() const
    {
        Int::Print();
        cout << "Object:: " << xval << endl;
    }
};
int main()
{
    Object obj;
    Object s1(obj);
    obj.Print();
    s1.Print();
}

 此时 Object s1(obj)操作只会调用派生类自定义拷贝构造函数,不会调动基类默认缺省拷贝构造函数,因为在派生类拷贝构造函数中没有明确指出调用,系统会调用基类默认缺省构造函数构建基类对象。

4. 派生类有自定义拷贝构造函数,基类也有

 如:

class Int
{
private:
    int value;
public:

    Int(int x = 1) : value{ x } {    }
    ~Int()     {     }
    Int(const Int& it) :value(it.value) {
        cout << "Copy Create Int " << this << endl;
    } 
    void Print() const {
        cout <<"Int:: "<< value << endl;
    }
};
class Object: public Int
{
private:
    int xval;
public:
    Object(int x = 0) :Int(x),xval(x + 10) {    }
    ~Object()    {    }
    Object(const Object& s): xval { s.xval }
    {
        cout << "Copy Create Object" << this << endl;
    }
    void Print() const
    {
        Int::Print();
        cout << "Object:: " << xval << endl;
    }
};
int main()
{
    
    Object obj;
    Object s1(obj);
    obj.Print();
    s1.Print();
}

 此情况和4相同,只会调用派生类自定义拷贝构造函数,不会调动基类默认缺省拷贝构造函数,因为在派生类拷贝构造函数中没有明确指出调用,系统会调用基类默认缺省构造函数构建基类对象。

 

此时要想正确进行拷贝,则需要在派生类的拷贝构造函数中明确指出调用基类拷贝构造函数

如:

class Int
{
private:
    int value;
public:

    Int(int x = 1) : value{ x } {    }
    ~Int()     {     }
    Int(const Int& it) :value(it.value) {
        cout << "Copy Create Int " << this << endl;
    } 
    void Print() const {
        cout <<"Int:: "<< value << endl;
    }
};
class Object: public Int
{
private:
    int xval;
public:
    Object(int x = 0) :Int(x),xval(x + 10) {    }
    ~Object()    {    }
    Object(const Object& s) : Int(s), xval { s.xval }
    {
        cout << "Copy Create Object" << this << endl;
    }
    void Print() const
    {
        Int::Print();
        cout << "Object:: " << xval << endl;
    }
};
int main()
{
    
    Object obj;
    Object s1(obj);
    obj.Print();
    s1.Print();
}

此时,便可以成功拷贝。

 

三、派生对象的拷贝赋值函数问题 

1.都没有拷贝赋值函数

如:

class Int
{
private:
    int value;
public:

    Int(int x = 1) : value{ x } {    }
    ~Int()     {     }
   
    void Print() const {
        cout <<"Int:: "<< value << endl;
    }
};
class Object: public Int
{
private:
    int xval;
public:
    Object(int x = 0) :Int(x),xval(x + 10) {    }
    ~Object()    {    }
  
    void Print() const
    {
        Int::Print();
        cout << "Object:: " << xval << endl;
    }
};
int main()

    Object obj;
    Object s1(66);
    obj = s1;
    obj.Print();
    s1.Print();
}

可以成功赋值;

 

2.基类有,派生类没有

如:

 

class Int
{
private:
    int value;
public:

    Int(int x = 1) : value{ x } {    }
    ~Int()     {     }
    Int& operator=(const Int& s)
    {
        if (&s != this)
        {
            this->value = s.value;
        }

   cout<<"Int::operator=() "<<endl;
        return *this;
    }

    void Print() const {
        cout <<"Int:: "<< value << endl;
    }
};
class Object: public Int
{
private:
    int xval;
public:
    Object(int x = 0) :Int(x),xval(x + 10) {    }
    ~Object()    {    }
  
    void Print() const
    {
        Int::Print();
        cout << "Object:: " << xval << endl;
    }
};
int main()

    Object obj;
    Object s1(66);
    obj = s1;
    obj.Print();
    s1.Print();
}

 可以成功赋值;

 3.派生类有,基类没有

如:

class Int
{
private:
    int value;
public:

    Int(int x = 1) : value{ x } {    }
    ~Int()     {     }
   
    void Print() const {
        cout <<"Int:: "<< value << endl;
    }
};
class Object: public Int
{
private:
    int xval;
public:
    Object(int x = 0) :Int(x),xval(x + 10) {    }
    ~Object()    {    }
  Object& operator=(const Object& s)
    {
        if (&s != this)
        {
            this->xval = s.xval;
        }
        cout<<"Object:: operator "<<endl;
        return *this;
     }
    void Print() const
    {
        Int::Print();
        cout << "Object:: " << xval << endl;
    }
};
int main()

    Object obj;
    Object s1(66);
    obj = s1;
    obj.Print();
    s1.Print();
}

不能成功赋值

4.都有拷贝赋值函数

如:

class Int
{
private:
    int value;
public:

    Int(int x = 1) : value{ x } {    }
    ~Int()     {     }
     Int& operator=(const Int& s)
    {
        if (&s != this)
        {
            this->value = s.value;
        }
        cout<<"Int::operator=() "<<endl;
        return *this;
    } 
    void Print() const {
        cout <<"Int:: "<< value << endl;
    }
};
class Object: public Int
{
private:
    int xval;
public:
    Object(int x = 0) :Int(x),xval(x + 10) {    }
    ~Object()    {    }
    Object& operator=(const Object& s)
    {
        if (&s != this)
        {
            this->xval = s.xval;
        }
        cout<<"Object:: operator "<<endl;
        return *this;
     } 
    void Print() const
    {
        Int::Print();
        cout << "Object:: " << xval << endl;
    }
};
int main()

    Object obj;
    Object s1(66);
    obj = s1;
    obj.Print();
    s1.Print();
}

 

 

没有显式调用基类拷贝赋值函数,不能成功赋值

解决方法:在派生类的拷贝赋值函数中显式调用基类拷贝赋值函数

如:

class Int
{
private:
    int value;
public:

    Int(int x = 1) : value{ x } {    }
    ~Int()     {     }
     Int& operator=(const Int& s)
    {
        if (&s != this)
        {
            this->value = s.value;
        }
        cout<<"Int::operator=() "<<endl;
        return *this;
    } 
    void Print() const {
        cout <<"Int:: "<< value << endl;
    }
};
class Object: public Int
{
private:
    int xval;
public:
    Object(int x = 0) :Int(x),xval(x + 10) {    }
    ~Object()    {    }
    Object& operator=(const Object& s)
    {
        if (&s != this)
        {
            //operator=(s);  错误写法,会无限递归
            Int::operator=(s);

            this->xval = s.xval;
        }
        cout<<"Object:: operator "<<endl;
        return *this;
     } 

    void Print() const
    {
        Int::Print();
        cout << "Object:: " << xval << endl;
    }
};
int main()

    Object obj;
    Object s1(66);
    obj = s1;
    obj.Print();
    s1.Print();
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值