Go最新赋值运算符重载详解(2),2024年最新最新Golang笔试题分享

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

~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
#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)
{
	Date temp(\*this);
	_day -= 1;
	return temp;
}

~Date()
{
	cout << "~Date():" << this << endl;
}

private:
int _year;
int _month;
int _day;
int* _p;
};

// 不能重载非法运算符----
// Date operator@(int a)
// {
// Date d;
// return d;
// }

// int operator+(int left, int right)
// {
// return left + right;
// }

void TestDate()
{
int a = 10;
int b = 20;
int c;
c = a + b;
a = b = c;

Date d1(2019, 3, 22);
d1.DateAdd(3);
d1 = d1 + 3;


Date d2(d1);
d2 = d1++;
d2 = ++d1;

Date d3(2018, 3, 22);
d3 = d3;
d3 = d1;
d3.operator=(d1);
d1 = d2 = d3;
d1.operator=(d2.operator=(d3));
if (d3 == d1)
	;

Date& d4 = d3;

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

转存中…(img-PaI5cGe4-1715900745638)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值