cpp——类——赋值操作符函数

本文深入探讨了C++中的赋值操作符函数,包括合成赋值操作符函数及其const修饰,以及与复制构造函数的区别。强调了赋值操作符函数在实例对象赋值时的作用,并阐述了编译器如何在未显式定义时自动合成这些函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

  • 赋值操作符函数模仿内置类型的赋值操作
  • 赋值操作符函数只是赋值操作符重载的一个特殊版本,其形参列表为[const] classname &,当实例对象赋值操作时调用

辅助类

class CAnimal
{
public:
    CAnimal() : mGroup(0)
    {
        cout << "CAnimal()" << endl;
    }
    
    CAnimal(int group) : mGroup(group)
    {
        cout << "CAnimal(" << group << ")" << endl;
    }
    
    CAnimal(const CAnimal &other) : mGroup(other.mGroup)
    {
        cout << "CAnimal(const CAnimal &other)" << endl;
    }
    
    ~CAnimal()
    {
        cout << "~CAnimal()" << endl;
    }
    
public:
    CAnimal& operator=(const CAnimal &other)
    {
        mGroup = other.mGroup;
        cout << "CAnimal operator=" << endl;
        return *this;
    }
    
private:
    int mGroup;
};

class CDog : public CAnimal
{
public:
    CDog() : mLoyal(10)
    {
        cout << "CDog()" << endl;
    }
    
    CDog(int loyal) : CAnimal(1), mLoyal(loyal)
    {
        cout << "CDog(" << loyal << ")" << endl;
    }
    
    CDog(const CDog &other) : CAnimal(other), mLoyal(other.mLoyal)
    {
        cout << "CDog(const CDog &other)" << endl;
    }
    
    ~CDog()
    {
        cout << "~CDog()" << endl;
    }
    
public:
    CDog& operator=(const CDog &other)
    {
        CAnimal::operator=(other);
        mLoyal = other.mLoyal;
        cout << "CDog operator=" << endl;
        return *this;
    }
    
private:
    int mLoyal;
};

class CCat : public CAnimal
{
public:
    CCat() : mCute(20)
    {
        cout << "CCat()" << endl;
    }
    
    CCat(int cute) : CAnimal(2), mCute(cute)
    {
        cout << "CCat(" << cute << ")" << endl;
    }
    
    CCat(const CCat &other) : CAnimal(other), mCute(other.mCute)
    {
        cout << "CCat(const CCat &other)" << endl;
    }
    
    ~CCat()
    {
        cout << "~CCat()" << endl;
    }
    
public:
    CCat& operator=(const CCat &other)
    {
        CAnimal::operator=(other);
        mCute = other.mCute;
        cout << "CCat operator=" << endl;
        return *this;
    }
    
private:
    int mCute;
};

class CPig : public CAnimal
{
public:
    CPig() : mWeight(30)
    {
        cout << "CPig()" << endl;
    }
    
    CPig(int weight) : CAnimal(3), mWeight(weight)
    {
        cout << "CPig(" << weight << ")" << endl;
    }
    
    CPig(const CPig &other) : CAnimal(other), mWeight(other.mWeight)
    {
        cout << "CPig(const CPig &other)" << endl;
    }
    
    ~CPig()
    {
        cout << "~CPig()" << endl;
    }
    
public:
    CPig& operator=(const CPig &other)
    {
        CAnimal::operator=(other);
        mWeight = other.mWeight;
        cout << "CPig operator=" << endl;
        return *this;
    }
    
private:
    int mWeight;
};

class CDonkey : public CAnimal
{
public:
    CDonkey() : mStrength(40)
    {
        cout << "CDonkey()" << endl;
    }
    
    CDonkey(int strength) : CAnimal(4), mStrength(strength)
    {
        cout << "CDonkey(" << strength << ")" << endl;
    }
    
    CDonkey(const CDonkey &other) : CAnimal(other), mStrength(other.mStrength)
    {
        cout << "CDonkey(const CDonkey &other)" << endl;
    }
    
    ~CDonkey()
    {
        cout << "~CDonkey()" << endl;
    }
    
public:
    CDonkey& operator=(const CDonkey &other)
    {
        CAnimal::operator=(other);
        mStrength = other.mStrength;
        cout << "CDonkey operator=" << endl;
        return *this;
    }
    
private:
    int mStrength;
};

class CHorse : public CAnimal
{
public:
    CHorse() : mSpeed(50)
    {
        cout << "CHorse()" << endl;
    }
    
    CHorse(int speed) : CAnimal(5), mSpeed(speed)
    {
        cout << "CHorse(" << speed << ")" << endl;
    }
    
    CHorse(const CHorse &other) : CAnimal(other), mSpeed(other.mSpeed)
    {
        cout << "CHorse(const CHorse &other)" << endl;
    }
    
    ~CHorse()
    {
        cout << "~CHorse()" << endl;
    }
    
public:
    CHorse& operator=(const CHorse &other)
    {
        CAnimal::operator=(other);
        mSpeed = other.mSpeed;
        cout << "CHorse operator=" << endl;
        return *this;
    }
    
private:
    int mSpeed;
};

class CFarmLand
{
public:
    CFarmLand() : mArea(5000)
    {
        cout << "CFarmLand()" << endl;
    }
    
    CFarmLand(int area) : mArea(area)
    {
        cout << "CFarmLand(" << area << ")" << endl;
    }
    
    CFarmLand(const CFarmLand &other) : mArea(other.mArea)
    {
        cout << "CFarmLand(const CFarmLand &other)" << endl;
    }
    
    ~CFarmLand()
    {
        cout << "~CFarmLand()" << endl;
    }
    
public:
    CFarmLand& operator=(const CFarmLand &other)
    {
        mArea = other.mArea;
        cout << "CFarmLand operator=" << endl;
        return *this;
    }
    
private:
    int mArea;
};

class CFarm : public CFarmLand
{
public:
    CFarm() : mCapacity(1000)
    {
        cout << "CFarm()" << endl;
    }
    
    CFarm(int capacity) : mCapacity(capacity), mDog(20), mCat(30), mPig(40), CFarmLand(8000)
    {
        cout << "CFarm(" << capacity << ")" << endl;
    }
    
    CFarm(const CFarm &other) : mCapacity(other.mCapacity), mDog(other.mDog), mCat(other.mCat), mPig(other.mPig), CFarmLand(other)
    {
        cout << "CFarm(const CFarm &other)" << endl;
    }
    
    ~CFarm()
    {
        cout << "~CFarm()" << endl;
    }
    
    CFarm& operator=(const CFarm &other)
    {
        CFarmLand::operator=(other);
        mCapacity = other.mCapacity;
        mPig = other.mPig;
        mDog = other.mDog;
        mCat = other.mCat;
        cout << "CFarm operator=" << endl;
        return *this;
    }
    
private:
    int mCapacity;
    
private:
    CPig mPig;
    CDog mDog;
    CCat mCat;
};

赋值操作符函数

void assign()
{
    cout << "-----construct-----" << endl;
    CFarm farm1;
    
    cout << "-----construct-----" << endl;
    CFarm farm2(80);
    
    cout << "-----assign-----" << endl;
    farm1 = farm2;
}
output:
-----construct-----
CFarmLand()
CAnimal()
CPig()
CAnimal()
CDog()
CAnimal()
CCat()
CFarm()
-----construct-----
CFarmLand(8000)
CAnimal(3)
CPig(40)
CAnimal(1)
CDog(20)
CAnimal(2)
CCat(30)
CFarm(80)
-----assign-----
CFarmLand operator=
CAnimal operator=
CPig operator=
CAnimal operator=
CDog operator=
CAnimal operator=
CCat operator=
CFarm operator=
~CFarm()
~CCat()
~CAnimal()
~CDog()
~CAnimal()
~CPig()
~CAnimal()
~CFarmLand()
~CFarm()
~CCat()
~CAnimal()
~CDog()
~CAnimal()
~CPig()
~CAnimal()
~CFarmLand()
结论:
  • 赋值操作符函数与一般赋值操作符重载无本质区别,只是赋值操作符重载的一个特殊版本而已,其形参列表为[const] classname &,当实例对象赋值操作时调用

合成赋值操作符函数

如果没有显式定义赋值操作符函数,编译器会合成赋值操作符函数,一旦显式定义了赋值操作符函数(即使没有实现),编译器不再合成赋值操作符函数,编译器认为开发者知道怎么赋值对象,不再需要编译器的帮助
合成赋值操作符函数形参列表为const classname &,父类调用赋值操作符函数,类类型成员调用赋值操作符函数,非类类型成员赋值,赋值顺序为父类(父类调用赋值操作符函数)->数据成员(类类型成员调用赋值操作符函数,非类类型成员赋值,赋值顺序与数据成员在类中定义顺序一致)
class CAdvanceFarm : public CFarm
{
private:
    CHorse mHorse;
    CDonkey mDonkey;
};
CAdvanceFarm没有显式定义赋值操作符函数,编译器会合成赋值操作符函数,合成赋值操作符函数为:
CAdvanceFarm& operator=(const CAdvanceFarm &other)
{
    CFarm::operator=(other);
    mHorse = other.mHorse;
    mDonkey = other.mDonkey;
    return *this;
}
void assign()
{
    cout << "-----construct-----" << endl;
    CAdvanceFarm farm1;
    
    cout << "-----construct-----" << endl;
    CAdvanceFarm farm2;
    
    cout << "-----assign-----" << endl;
    farm1 = farm2;
}
output:
-----construct-----
CFarmLand()
CAnimal()
CPig()
CAnimal()
CDog()
CAnimal()
CCat()
CFarm()
CAnimal()
CHorse()
CAnimal()
CDonkey()
-----construct-----
CFarmLand()
CAnimal()
CPig()
CAnimal()
CDog()
CAnimal()
CCat()
CFarm()
CAnimal()
CHorse()
CAnimal()
CDonkey()
-----assign-----
CFarmLand operator=
CAnimal operator=
CPig operator=
CAnimal operator=
CDog operator=
CAnimal operator=
CCat operator=
CFarm operator=
CAnimal operator=
CHorse operator=
CAnimal operator=
CDonkey operator=
~CDonkey()
~CAnimal()
~CHorse()
~CAnimal()
~CFarm()
~CCat()
~CAnimal()
~CDog()
~CAnimal()
~CPig()
~CAnimal()
~CFarmLand()
~CDonkey()
~CAnimal()
~CHorse()
~CAnimal()
~CFarm()
~CCat()
~CAnimal()
~CDog()
~CAnimal()
~CPig()
~CAnimal()
~CFarmLand()
注:对于合成赋值操作符函数,必须确保父类和类类型成员能调用赋值操作符函数,否则编译错误,因为对象的部分数据得不到合适的赋值

赋值操作符函数形参的const修饰

class CAnimal
{
public:
    CAnimal() : mGroup(0)
    {
        cout << "CAnimal()" << endl;
    }
    
    CAnimal(int group) : mGroup(group)
    {
        cout << "CAnimal(" << group << ")" << endl;
    }
    
    CAnimal(CAnimal &other) : mGroup(other.mGroup)
    {
        cout << "CAnimal(const CAnimal &other)" << endl;
    }
    
public:
    CAnimal& operator=(CAnimal &other)
    {
        mGroup = other.mGroup;
        cout << "CAnimal operator=" << endl;
        return *this;
    }
    
private:
    int mGroup;
};
void assign()
{
    cout << "-----construct-----" << endl;
    CAnimal farm1;
    
    cout << "-----construct-----" << endl;
    const CAnimal farm2(1);
    
    cout << "-----assign-----" << endl;
    //farm1 = farm2;
}
自定义赋值操作符函数的形参不带const修饰时,编译器不再合成const版本的赋值操作符函数,编译器认为开发者知道怎么赋值对象,不再需要编译器的帮助
赋值操作符函数总结:
  • 赋值操作符函数与一般赋值操作符重载无本质区别,只是赋值操作符重载的一个特殊版本而已,其形参列表为[const] classname &,当实例对象赋值操作时调用
  • 合成赋值操作符函数的形参列表为const classname &
  • 合成赋值操作符函数赋值顺序为父类(父类调用赋值操作符函数)->数据成员(类类型成员调用赋值操作符函数,非类类型成员赋值,赋值顺序与数据成员在类中定义顺序一致)
  • 显式赋值操作符函数如果形参列表声明为classname &,编译器不再合成赋值操作符函数,编译器认为开发者知道怎么赋值对象,不再需要编译器的帮助

复制构造函数VS赋值操作符函数

相同点:
  • 复制构造函数和赋值操作符函数都有编译器合成版本,当没有显式定义复制构造函数和赋值操作符函数时,编译器会合成复制构造函数和赋值操作符函数
  • 复制构造函数和赋值操作符函数的合成版本形参列表为const classname &
  • 复制构造函数和赋值操作符函数的合成版本值复制(浅复制)和值赋值(浅赋值)
  • 复制构造函数和赋值操作符函数的合成版本复制和赋值顺序一致,复制和赋值顺序为父类(父类调用复制构造函数和赋值操作符函数)->数据成员(类类型成员调用复制构造函数和赋值操作符函数,非类类型成员复制初始化和赋值,数据成员复制和赋值顺序与数据成员在类中定义顺序一致)
  • 如果显式复制构造函数和显式赋值操作符函数形参列表声明为classname &,编译器不再合成复制构造函数和赋值操作符函数,编译器认为开发者知道怎么复制和赋值对象,不再需要编译器的帮助
不同点:
  • 复制构造函数模仿内置类型复制初始化,赋值操作符函数模仿内置类型赋值操作,这就决定了复制构造函数和赋值操作符函数的本质区别:复制构造函数是初始化行为,因此复制构造函数至多调用一次,对象生成(定义)时调用,赋值操作符函数是赋值行为,因此赋值操作符函数可调用多次,实例对象生成(定义)后调用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值