C++FAQ

Tuesday, February 07, 2006

 

如何overload postfix的++以及--運算子?

Q:如何overload postfix的++以及--運算子? (像是x++, x--) ?

A: 必須以一個 int 做為參數:

class MyClass
{
public:
MyClass& operator++(); //Prefix increment operator (++x)
MyClass operator++(int); //Postfix increment operator (x++)

MyClass& operator--(); //Prefix decrement operator (--x)
MyClass operator--(int); //Postfix decrement operator (x--)
};


Comments: Post a Comment



<< Home

Archives

January 2006   February 2006  

This page is powered by Blogger. Isn't yours?