jnjejnx 2021-01-04 07:43 采纳率: 100%
浏览 22
已采纳

这段代码结果为什么没有输出?

 

#include<iostream>
using namespace std;
//递增运算符重载 ++
//自定义的整型
class MyInteger {
	friend ostream &operator<<(ostream& cout, MyInteger myint);
public:
	MyInteger() {
		m_Num = 0;
	}
	//重载++前置运算符
	MyInteger& operator++() {//返回引用是为了对一个数据进行操作 引用起别名后指向的是一个数据

		m_Num++;//先进行++运算
		return *this;//再将自身做返回
	}
	//重载后置++运算符
	//void operator++(int)  int代表占位参数,可以用于区分前置和后置递增
	MyInteger operator++(int) {
		//先返回结果
		MyInteger temp = *this;
		
		//后递增
		m_Num++;
		
		//最后将记录的结果返回
		return temp;
	}


private:
	int m_Num;
};
//重载<<运算符
ostream& operator<<(ostream &cout, MyInteger myint) {
	cout << myint.m_Num ;
	return cout;
}
void test01() {
	MyInteger myint;

	cout << ++(++myint) << endl;
	cout << myint << endl;
}
void test02() {
	MyInteger myint;
	cout << myint++ << endl;
	cout << myint << endl;
}

int main()
{
	//test01();
	//int a = 0;
	//cout << ++(++a) << endl;
	//cout << a << endl;
	test02;
	system("pause");
	return 0;	
}

 

  • 写回答

1条回答 默认 最新

  • wang_hs2002 2021-01-04 09:35
    关注

    main函数中,调用test02函数,必须要带(),也就是test02(),带上()就能正常执行test02函数了。

    为什么没有()编译程序没有报错,还能正常运行,就不清楚什么原因了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题