现在的位置: 首页 > 综合 > 正文

operator++ 重载

2014年02月28日 ⁄ 综合 ⁄ 共 331字 ⁄ 字号 评论关闭
class AA
{
private:
	int a;
public:
	AA():a(0){};
	AA& operator ++()// prefix
	{
		cout <<"++AA"<<endl;
		++a;
		return *this;
	}

	AA operator ++(int)// suffix
	{
		cout <<"AA++"<<endl;
		AA temp = *this;
		++a;
		return temp;
	}

	void show()
	{
		cout<<"int: "<<a<<endl;
	}
};

int _tmain(int argc, char* argv[])
{

	for (size_t i=10; i--!=0;)
	{
		cout<<i<<endl;// 9 8 7 6 5 4 3 2 1 0
	}

	AA t;
	++t;
	t.show();
	t++;
	t.show();

	return 0;
}

抱歉!评论已关闭.