这篇博客往后五篇,我将介绍一下什么叫做运算符重载
运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
1、前置递增与后置递增
我们先来看一下加号运算符重载的两种情况,即前置运算符递增和后置运算符递增。
上一段程序:
#include<iostream>
#include<string>
using namespace std;
void test01()
{
int a = 10;
cout << a++ << endl;//先计算本身
cout << a << endl;//在计算+1
int b = 10;
cout << ++b << endl;//计算+1之后的值,打印出来
cout << b << endl;//已经+1过了。
}
int main()
{
test01();
system("pause");
return 0;
}
最后的运算结果是什么呢?我们运行一下。
10
11
11
11
请按任意键继续