赋值运算符函数

成员函数形式的运算符声明和实现与成员函数类似,首先应当在类定义中声明该运算符,声明的具体形式为:
返回类型  operator 运算符(参数列表);
既可以在类定义的同时定义运算符函数使其称为inline型,也可以在类定义之外定义运算符函数,但要使用作用域限定符“::”,类外定义的基本格式为:
返回类型  类名::operator 运算符(参数列表)
{……}
1、赋值运算是一种很常见的运算, 如果不重载赋值运算符,编译器会自动为每个类生成一个缺省的赋值运算符重载函数  对象1=对象2(隐性转换); 完成了由对象2各个成员到对象1相应成员的复制,其中包括指针成员, 如果对象1中含指针成员,并且牵扯到类内指针成员动态申请内存时,析构函数释放资源就会出现问题
              

#include <iostream>
using namespace std;
class point
{
        private:
                int ix;
                int iy;
        public:
                //构造函数只能显示调用,不能=
                explicit point(int ix1,int iy1)
                :ix(ix1)
                ,iy(iy1)
                {
                        cout<<"构造函数初始化:"<<endl;       
                }
                //赋值构造函数
                point & operator=(const point &rhs)
                {
                        ix=rhs.ix;
                        iy=rhs.iy;
                        cout<<"重载=运算符:"<<endl;
                }
                //复制构造函数
                point (const point &rhs) 
                :ix(rhs.ix)
                ,iy(rhs.iy)
                {
                        cout<<"拷贝构造函数:"<<endl;
                }
                void print()
                {
                        cout<<"("<<ix<<","<<iy<<")"<<endl;
                }
};

int main()
{
        point p1(1,2);
        p1.print();

        point p2=p1;      
//复制构造函数(隐式),p2之前没有创建
        p2.print();

        point p3(p1);
        p3.print();

        point p4(3,4);
        p4.print();

        p1=p4;            //赋值操作,p1之前已经创建
        p1.print();
}
vvVeQAAfEf+D3N7VWthVt3XAAAAAElFTkSuQmCC




///
/// @file    computer3.cpp
/// @author  meihao1203(meihao19931203@outlook.com)
/// @date    2017-12-17 14:15:36
///

#include<iostream>
#include<string.h>
using namespace std;
class computer
{
        char *_brand;
        float _price;
        public:
        computer(const char* brand,float price)
                :_price(price)
        {
                _brand = new char[strlen(brand)+1];
                strcpy(_brand,brand);
        }
        ~computer()
        {
                        delete []_brand;
                        _brand = NULL;
                        cout<<"~computer()"<<endl;
        }
        computer(const computer &rhs)
                :_price(rhs._price)
        {
                _brand = new char[strlen(rhs._brand)+1];
                strcpy(_brand,rhs._brand);
                cout<<"computer(const computer &)"<<endl;
        }
#if 0
        computer & operator=(const computer &rhs)  //简单的修改指针,会导致内存泄露;析构函数多次释放段错误
        {
                this->_brand = rhs._brand;
                this->_price = rhs._price;
                cout<<"computer & operator = (const computer &)"<<endl;
        }
#endif

        computer & operator = (const computer &rhs)
        {
                delete []_brand;
                _brand = NULL;
                _brand = new char[strlen(rhs._brand)+1];
                strcpy(_brand,rhs._brand);
                _price = rhs._price;
                return *this;   //返回本身
        }
        void print()
        {
                cout<<"品牌:"<<_brand<<endl;
                cout<<"价格:"<<_price<<endl;
        }
};
int main()
{
        computer cr1("IBM",1234.56);
        cr1.print();
        computer cr2("ASUS",7890.12);
        cr2.print();
        cr1 = cr2;   //computer.operator=(cr2)
        cr1.print();
        return 0;
}

RLAQAAAKyHWAoAAABgvf8DJaUC3H4xx8YAAAAASUVORK5CYII=


















转载于:https://www.cnblogs.com/meihao1203/p/8793042.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值