cout,printf的++,--优先问题

#include<iostream>
using namespace std;
int n;
class myint {
	friend ostream& operator<<(ostream& cout, myint mi);
private:
	int mynum;
	string name;
public:
	myint() {
		mynum = 0;
		name = "*********";
	}
	myint& operator++() {
		n++;
		mynum++;
		cout <<"第"<<n<<"次"<< "前置++" << endl;
		return *this;
	}
	myint& operator--() {
		n++;
		mynum--;
		cout << "第" << n << "次" << "前置--" << endl;
		return *this;
	}
	myint operator++(int) {
		n++;
		myint temp = *this;
		mynum++;
		cout << "第" << n << "次" << "后置++" << endl;
		return temp;
	}
	myint operator--(int) {
		n++;
		myint temp = *this;
		mynum--;
		cout << "第" << n << "次" << "后置--" << endl;
		return temp;
	}
};
ostream& operator<<(ostream& cout, myint me) {
	n++;
	cout <<"a="<< me.mynum << me.name <<"n="<< n<<endl;
	return cout;
}
int main() {
	myint me;
	cout << --me << me++  << me-- << ++me << endl;
	int a = 0;
	
}

第1次前置++
第2次后置–
第3次后置++
第4次前置–
a=0*********n=5
a=0*********n=6
a=1*********n=7
a=1*********n=8

非常的有意思,直接说结论,++,- -前置和后置,4种,根据语句,从右往左进行,压栈,最后输出,出栈
在这里插入图片描述

posted @ 2021-05-11 19:13  cheems~  阅读(25)  评论(0编辑  收藏  举报