重载赋值运算符 && 对象

class CMessage
{
private:
    char * m_pMessage;

public:
    void showIt()const
    {
        cout << m_pMessage << endl;
    }
    //构造函数
    CMessage(const char* text="Default message")
    {
        cout << "Constructor difinition" << endl;

        size_t length{strlen(text)+1};
        m_pMessage = new char[length+1];
        strcpy_s(m_pMessage,length+1,text);
    }
    //复制构造函数
    CMessage(const CMessage & aMess)
    {
        size_t len{strlen(aMess.m_pMessage)+1};
        this->m_pMessage = new char[len];
        strcpy_s(m_pMessage,len,aMess.m_pMessage);
    }
    //重载赋值运算符
    CMessage & operator=(const CMessage & aMess)
    {
        if (this!= &aMess)
        {
            delete[]m_pMessage;
            size_t length{strlen(aMess.m_pMessage)+1};
            m_pMessage = new char[length];
            strcpy_s(this->m_pMessage,length,aMess.m_pMessage);
        }
        return *this;
    }

    //析构函数
    ~CMessage()
    {
        cout << "Destructor called" << endl;
        delete[]m_pMessage;
    }
};

int main()
{
    CMessage motto1{"Amiss is as good as a mile"};
    CMessage motto2;

    motto2 = motto1;

    motto2.showIt();
    motto1.showIt();
    
    return 0;
}

复制构造函数:

当某个类动态的为数据成员分配空间,又利用按值传递给函数传递该类的对象,那么必须要实现复制构造函数

如:

  motto2 {motto1};

  CMessage & displayMessage(CMessage localMsg)
   {
    cout <<"the message is:----------------" << endl;
    localMsg.showIt();
    return *this;
   }

都会出现异常,如果没有实现复制构造函数,即两个对象的指针指向了同一块地址区域。

重载赋值运算符:

以现有的同类对象进行初始化类的对象,或者通过按值传递方式给函数传递对象,调用默认复制构造函数。

当赋值语句的左边和右边是同类类型的对象时,调用默认赋值运算符。

如:

  motto2 = motto1;

如果我们没有实现赋值运算符函数,编译器就会使用默认的赋值运算符函数,当为数据成员动态的分配内存,进行对象赋值,就会程序异常。

因为两个对象都有一个指向相同内存地址的指针。

分析赋值运算符函数:

  CMessage & operator=(const CMessage & aMess)

 {    if (this!= &aMess)

     {

      delete[]m_pMessage;

      size_t length{strlen(aMess.m_pMessage)+1};

        m_pMessage = new char[length];

      strcpy_s(this->m_pMessage,length,aMess.m_pMessage);

      }

  return *this;

}

delete[]m_pMessage; 删除分配给第一个对象的内存,重新分配足够的内存,以容纳第二个对象的字符串。

1、当 motto1 = motto1;会发生什么呢?

赋值运算符函数会释放 motto1 对象成员指向的内存

所以,if (this!= &aMess) 这条语句,是有必要的。

2、那么返回对象为什么?

motto1 = motto2=motto3;

这条语句的原型是:

    motto1.operator=(motto2.operator=(motto3));

可知 motto2.operator=(motto3) 的结果必须是对象才能作为 motto1.operator=()的参数。

3、那么返回引用为什么?

(motto1 = motto2)=motto3;

这条语句的原型是:

    (motto1.operator=(motto2)).operator=(motto3);

可知(motto1.operator=(motto2))的结果是个是个返回的临时对象,它是 rvalue ,编译器不允许使用 rvalue 调用函数成员

而返回引用它是 lvalue。

转载于:https://www.cnblogs.com/yunqie/p/5935658.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值