重载一元运算符

自增++运算符

cpp 复制代码
#include<iostream>
using namespace std;
class CGirl
{
public:
	string name;
	int ranking;
	CGirl() { name = "zhongge"; ranking = 5; }
	void show() const{ cout << "name : "<<name << " , ranking : " << ranking; }

};
int main() {
	CGirl g1;
	g1.show();
	return 0;
}

现在我们重载一个++运算符

cpp 复制代码
#include<iostream>
using namespace std;
class CGirl
{
public:
	string name;
	int ranking;
	CGirl() { name = "zhongge"; ranking = 5; }
	void show() const{ cout << "name : "<<name << " , ranking : " << ranking; }
	void operator++() {
		ranking++;
	}

};
int main() {
	CGirl g1;
	++g1;
	g1.show();
	return 0;
}

main函数里的g1++不行但是++g1就行了;

然而你++(++g)不行,你让你重载函数返回对象的引用就可以了;

cpp 复制代码
#include<iostream>
using namespace std;
class CGirl
{
public:
	string name;
	int ranking;
	CGirl() { name = "zhongge"; ranking = 5; }
	void show() const{ cout << "name : "<<name << " , ranking : " << ranking; }
	 CGirl & operator++() {
		ranking++;
		return *this;
	}

};
int main() {
	CGirl g1;
	++(++g1);
	g1.show();
	return 0;
}

上面这是自增运算符的前置,我们再来个后置的;

c++规定重载自增&自减运算符,如果重载函数有一个int形参,编译器处理后置表达式时将调用这个重载函数。

cpp 复制代码
#include<iostream>
using namespace std;
class CGirl
{
public:
	string name;
	int ranking;
	CGirl() { name = "zhongge"; ranking = 5; }
	void show() const{ cout << "name : "<<name << " , ranking : " << ranking; }
	 CGirl & operator++(int ) {
		ranking++;
		return *this;
	}

};
int main() {
	CGirl g1;
	g1++;
	g1.show();
	return 0;
}

这样就ok了;


整数的话不可以后自增嵌套,前自增嵌套可以;

cpp 复制代码
#include<iostream>
using namespace std;
class CGirl
{
public:
	string name;
	int ranking;
	CGirl() { name = "zhongge"; ranking = 5; }
	void show() const{ cout << "name : "<<name << " , ranking : " << ranking; }
	CGirl& operator++(int ) {
		ranking++;
		return *this;
	}


};
int main() {
	CGirl g1,g2;
	g2=g1++;
	g2.show();
	g1.show();
	return 0;
}
cpp 复制代码
name : zhongge , ranking : 6name : zhongge , ranking : 6
C:\Users\33007\source\repos\ConsoleApplication8\x64\Debug\ConsoleApplication8.exe (进程 9820)已退出,代码为 0。
按任意键关闭此窗口. . .

显然你g1++是后来加,g2应该是g1之前的值而不是增后的值。

所以改一下后++的代码;

cpp 复制代码
CGirl operator++(int ) {
	CGirl tmp = *this;
	ranking++;
	return tmp;
}

函数的返回值不能是引用,成员函数的临时对象不能引用;

cpp 复制代码
name : zhongge , ranking : 5name : zhongge , ranking : 6
C:\Users\33007\source\repos\ConsoleApplication8\x64\Debug\ConsoleApplication8.exe (进程 15512)已退出,代码为 0。
按任意键关闭此窗口. . .

这样功能就对上了;

相关推荐
We་ct4 分钟前
LeetCode 212. 单词搜索 II:Trie+DFS 高效解法
开发语言·算法·leetcode·typescript·深度优先·图搜索算法·图搜索
样例过了就是过了7 分钟前
LeetCode热题100 路径总和 III
数据结构·c++·算法·leetcode·链表
lxh01137 分钟前
函数防抖题解
前端·javascript·算法
OxyTheCrack9 分钟前
【C++】简述main函数中的argc与argv
开发语言·c++
再难也得平12 分钟前
力扣41. 缺失的第一个正数(Java解法)
数据结构·算法·leetcode
颜酱12 分钟前
环检测与拓扑排序:BFS/DFS双实现
javascript·后端·算法
历程里程碑16 分钟前
Linux 49 HTTP请求与响应实战解析 带http模拟实现源码--万字长文解析
java·开发语言·网络·c++·网络协议·http·排序算法
ZVAyIVqt0UFji17 分钟前
高可用虚拟IP(HaVip)技术详解:原理、设计与应用
开发语言·网络·网络协议·tcp/ip·perl
飞Link18 分钟前
深度解析 TS2Vec:时序表示学习中的层次化建模(Hierarchical Contrastive Learning)
开发语言·python·学习·数据挖掘
IronMurphy19 分钟前
【算法二十】 114. 寻找两个正序数组的中位数 153. 寻找旋转排序数组中的最小值
java·算法·leetcode