C++学习-派生类的复制运算符

本文深入探讨了派生类赋值运算符的实现及其与基类成员的交互过程,通过实例展示了如何正确处理派生类中基类部分的赋值,确保赋值操作的一致性和准确性。

派生类的赋值控制函数

        派生类构造函数在其初始化阶段不仅要初始化类自己的成员,还负责初始化派生类对象的基类部分。因此,派生类的复制控制函数在复制自有成员的同时,也要复制基类部分的成员。类似的,如果派生类的赋值运算符没有处理基类的相应的部分,则派生类中基类的部分会采用默认值,请看下面的程序

#include <iostream>

using namespace std;

class Base
{
public:
    Base() { p1 = new int; *p1 = 1; cout << "Base::Base()" << endl; }
    ~Base() { delete p1; cout << "Base::~Base()" << endl; }
    Base& operator=(const Base &b) { cout << "Base::operator=()" << endl; p1 = new int; *p1 = *b.p1; return *this; }
    int *p1;
};

class Derived : public Base
{
public:
    Derived() { p2 = new int; *p2 = 2; cout << "Derived::Derived()" << endl; }
    ~Derived() { delete p2; cout << "Derived::~Derived()" << endl; }
	Derived & operator=(Derived &d) { cout << "Derived::operator=()" << endl; p2 = new int; *p2 = *d.p2; return *this;}
    int *p2;
};

int main(void)
{
    Derived d;
    *d.p1 = 3;
    *d.p2 = 4;

    cout << "d.p1: " << *d.p1 << endl;
    cout << "d.p2: " << *d.p2 << endl;

	//Derived d2 = d; //注意:此时调用的不是赋值运算符,而是d2的复制构造函数!!!
	Derived d2;
	d2 = d;
    cout << "d2.p1: " << *d2.p1 << endl;
    cout << "d2.p2: " << *d2.p2 << endl;

    *d2.p1 = 5;
    *d2.p2 = 6;
    cout << endl;
    cout << "d.p1: " << *d.p1 << endl;
    cout << "d.p2: " << *d.p2 << endl;
    cout << "d2.p1: " << *d2.p1 << endl;
    cout << "d2.p2: " << *d2.p2 << endl;

    return 0;
}
输出结果为

        在没有给d2中的p1和p2重新赋值前,d2的*p1为1而不是为3,显然和我们想要的结果不一致。。。

派生类的赋值运算符也必须地显式为基类部分赋值:

//Base::operator=(const Base&)不会被自动调用
Derived &Derived::operator=(const Derived &d)
{
	Base::operator=(d); //为基类部分赋值
	p2 = new int; 
	*p2 = *d.p2;
	return *this;
}

更改后的完整程序

#include <iostream>

using namespace std;

class Base
{
public:
    Base() { p1 = new int; *p1 = 1; cout << "Base::Base()" << endl; }
    ~Base() { delete p1; cout << "Base::~Base()" << endl; }
    Base& operator=(const Base &b) { cout << "Base::operator=()" << endl; p1 = new int; *p1 = *b.p1; return *this; }
    int *p1;
};

class Derived : public Base
{
public:
    Derived() { p2 = new int; *p2 = 2; cout << "Derived::Derived()" << endl; }
    ~Derived() { delete p2; cout << "Derived::~Derived()" << endl; }
	Derived & operator=(Derived &d) { cout << "Derived::operator=()" << endl; p2 = new int; *p2 = *d.p2; return *this;}
    int *p2;
};

int main(void)
{
    Derived d;
    *d.p1 = 3;
    *d.p2 = 4;

    cout << "d.p1: " << *d.p1 << endl;
    cout << "d.p2: " << *d.p2 << endl;

	//Derived d2 = d; //注意:此时调用的不是拷贝控制函数,而是d2的默认复制构造函数
	Derived d2;
	d2 = d;
    cout << "d2.p1: " << *d2.p1 << endl;
    cout << "d2.p2: " << *d2.p2 << endl;

    *d2.p1 = 5;
    *d2.p2 = 6;
    cout << endl;
    cout << "d.p1: " << *d.p1 << endl;
    cout << "d.p2: " << *d.p2 << endl;
    cout << "d2.p1: " << *d2.p1 << endl;
    cout << "d2.p2: " << *d2.p2 << endl;

    return 0;
}
这样就和我们想要的结果一致了大笑


参考

        1、《C++ Prime》 第5版 15.7 构造函数与拷贝控制

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值