网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
赋值运算符重载详解
为什么要引入赋值运算符重载这个概念
- 针对上面的这种情况,调用函数来进行比较,肯定是没有直接用运算符号进行比较来的直接,所以说,引入运算符重载可以提高代码的可读性,使用起来也会更加的方便一些
- 用户可以让编译器按照指定的规则对自定义类型对象直接进行一些运算符的操作
- 对>运算符的重载
赋值运算符重载的概念
- C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似
- 函数名字为:关键字operator后面接需要重载的运算符符号。
- 函数原型:返回值类型 operator操作符(参数列表)
下面通过代码进行展示
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
cout << "Date(int,int,int):" << this << endl;
}
// Date(const Date& d)
// {
// \_year = d.\_year;
// \_month = d.\_month;
// \_day = d.\_day;
// cout << "Date(Date&):" << this << endl;
// }
~Date()
{
cout << "~Date():" << this << endl;
}
private:
int _year;
int _month;
int _day;
};
void TestDate()
{
Date d1(2020, 5, 5);
Date d2(d1);
Date d3(2019, 5, 5); //本来希望d3的内容给成和d1内容是一样的,但是由于输入的时候输入错了
//所以我们利用赋值,把d1中的内容赋值给d3
//通过调试看出代码没有任何的问题
d3 = d1;
}
int main()
{
TestDate();
return 0;
}
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class String
{
public:
String(const char\* str = "")
{
cout << "String(const char\* ):" << this << endl;
if (nullptr == str)
str = "";
_str = (char\*)malloc(strlen(str) + 1);
strcpy(_str, str);
}
// 拷贝构造函数必须显式提供
~String()
{
cout << "~String():" << this << endl;
free(_str);
_str = nullptr;
}
private:
char\* _str;
};
void TestString()
{
String s1("hello");
String s2;
s2 = s1; //利用赋值的操作
//通过监视可以看出其实s1和s2还是公用的同一块内存空间,所以在资源释放的时候还是会
//出现浅拷贝的问题
//而且这样子直接赋值的话,还会导致s2的空间没有释放
//因为是直接把s1的地址复制到s2上面去的,所以造成了内存泄露
//所以对于string类的赋值还是需要我们自己去完成
}
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
cout << "Date(int,int,int):" << this << endl;
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
cout << "Date(Date&):" << this << endl;
}
// d1 = d2 = d3;
Date& operator=(const Date& d)
{
if (this != &d) //意思就是如果是自己给自己赋值的话,就不需要来做了
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return \*this;
}
// \*this + day
// 问题:加完之后的结果可能是非法日期???
Date operator+(int day)
{
Date temp(\*this);
temp._day += day;
return temp;
}
//先给一个临时变量temp,然后再去修改临时变量
//temp中内容,然后返回temp,要知道,现在不是以引用的方式返回的
//是以值得方式返回的
// 首先返回引用
// 返回引用是否可行---->引用作为函数的返回值类型:不能返回函数栈上的空间
Date& DateAdd(int day)
{
_day += day;
return \*this;
}
bool operator==(const Date& d)
{
return _year == d._year &&
_month == d._month &&
_day == d._day;
}
bool operator!=(const Date& d)
{
return !(\*this == d);
}
// 前置++
// d2 = ++d1
Date& operator++()
{
_day += 1;
return \*this;
}
// 后置++
// d2 = d1++
Date operator++(int)
{
Date temp(\*this);
_day += 1;
return temp;
}
Date& operator--()
{
_day -= 1;
return \*this;
}
Date operator--(int)
![img](https://img-blog.csdnimg.cn/img_convert/dede6d7c8a07d0fe43e098d14934cc93.png)
![img](https://img-blog.csdnimg.cn/img_convert/9bbcc421f721807bd7fd21f100779447.png)
![img](https://img-blog.csdnimg.cn/img_convert/d4db3a8721de09b2a172e3d3f59958e8.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618658159)**
转存中...(img-GjNBlDaE-1715652553276)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618658159)**