自增自减运算符重载

#include <iostream>
using namespace std;

class counter
{
public:
	counter(){v = 0;}
	counter & operator ++();
	counter & operator ++(int);
	void print(){ cout<<v<<endl;}
private:
	unsigned v;
};

counter & counter::operator++()
{
	v++;
	return *this;
}

counter & counter::operator++(int) //为后置的
{
	counter t;
	t.v = v++;
	return t;
}

int main(void)
{
	counter c;
	for(int i = 0;i< 8;i++)
		c++;
	c.print();

	for(i = 0; i< 8;i++)
		++c;
	c.print();
	return 0;
}

你可能感兴趣的:(自增自减运算符重载)