重载一元运算符

自增++运算符

#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;
}

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

#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)不行,你让你重载函数返回对象的引用就可以了;

#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形参,编译器处理后置表达式时将调用这个重载函数。

#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了;

-----------------------------------------------------------------------------------------------------------------------

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

#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;
}

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

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

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

CGirl operator++(int ) {
	CGirl tmp = *this;
	ranking++;
	return tmp;
}

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

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

这样功能就对上了;

相关推荐

  1. C++ 二运算符重载

    2024-07-09 21:00:05       48 阅读
  2. 掘根宝典之C++友函数与运算符重载

    2024-07-09 21:00:05       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 21:00:05       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 21:00:05       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 21:00:05       58 阅读
  4. Python语言-面向对象

    2024-07-09 21:00:05       69 阅读

热门阅读

  1. QT 设置控件的展开和消失

    2024-07-09 21:00:05       23 阅读
  2. qt 读取配置文件

    2024-07-09 21:00:05       22 阅读
  3. 王道考研数据机构:中缀表达式转为后缀表达式

    2024-07-09 21:00:05       31 阅读
  4. 基于深度学习的夜间图像修复

    2024-07-09 21:00:05       24 阅读
  5. SQL AND & OR 运算符的使用与区别

    2024-07-09 21:00:05       20 阅读
  6. VSCode中常用的快捷键

    2024-07-09 21:00:05       19 阅读
  7. C# —— File文件读写

    2024-07-09 21:00:05       24 阅读