关闭

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

标签: 派生类赋值运算符赋值运算符
455人阅读 评论(0) 收藏 举报
分类:

派生类的赋值控制函数

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

#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 构造函数与拷贝控制

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:89918次
    • 积分:2011
    • 等级:
    • 排名:第19090名
    • 原创:112篇
    • 转载:15篇
    • 译文:1篇
    • 评论:8条
    博客专栏
    最新评论