码迷,mamicode.com
首页 > 其他好文 > 详细

++的前置和后置

时间:2014-09-07 14:41:45      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:blog   os   io   ar   div   sp   log   on   c   

#include <iostream>
using namespace std;
class Test
{
private:
	int num;
public:
	Test():num(0)
	{}
	Test& operator=(const int &num)
	{
		this->num = num;
		return *this;
	}
	Test& operator=(const Test &test)
	{
		this->num = test.num;
		return *this;
	}
	Test operator++(int)//后置
	{
		Test temp = *this;
		++(this->num);
		return temp;
	}

	Test& operator++()//前置,返回的是引用
	{
		++(this->num);
		return *this;
	}

	operator int()//向int转换
	{
		return num;
	}

};

int main()
{
	Test t1,t2;
	
	int nt1 = ++t1;
	cout<<"应该是1,实际是"<<nt1<<endl;//输出1
	int nt2 = t2++;
	cout<<"应该是0,实际是"<<nt2<<endl;//输出0
	return 0;
}

  

++的前置和后置

标签:blog   os   io   ar   div   sp   log   on   c   

原文地址:http://www.cnblogs.com/dy-techblog/p/3960351.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!