慢慢看吧!MSDN中的文章,不错。Increment and Decrement
The increment and decrement operators fall into a special category because there are two variants of each: Preincrement and postincrement
Predecrement and postdecrement 
When you write overloaded operator functions, it can be useful to implement separate versions for the prefix and postfix versions of these operators. To distinguish between the two, the following rule is observed: The prefix form of the operator is declared exactly the same way as any other unary operator; the postfix form accepts an additional argument of type int.Important   When specifying an overloaded operator for the postfix form of the increment or decrement operator, the additional argument must be of type int; specifying any other type generates an error.The following example shows how to define prefix and postfix increment and decrement operators for the Point class:class Point
{
public:
   // Declare prefix and postfix increment operators.
   Point& operator++();       // Prefix increment operator.
   Point operator++(int);     // Postfix increment operator.   // Declare prefix and postfix decrement operators.
   Point& operator--();       // Prefix decrement operator.
   Point operator--(int);     // Postfix decrement operator.   // Define default constructor.
   Point() { _x = _y = 0; }   // Define accessor functions.
   int x() { return _x; }
   int y() { return _y; }
private:
   int _x, _y;
};// Define prefix increment operator.
Point& Point::operator++()
{
   _x++;
   _y++;
   return *this;
}// Define postfix increment operator.
Point Point::operator++(int)
{
   Point temp = *this;
   ++*this;
   return temp;
}// Define prefix decrement operator.
Point& Point::operator--()
{
   _x--;
   _y--;
   return *this;
}// Define postfix decrement operator.
Point Point::operator--(int)
{
   Point temp = *this;
   --*this;
   return temp;
}The same operators can be defined in file scope (globally) using the following function heads:friend Point& operator++( Point& )      // Prefix increment
friend Point& operator++( Point&, int ) // Postfix increment
friend Point& operator--( Point& )      // Prefix decrement
friend Point& operator--( Point&, int ) // Postfix decrementThe argument of type int that denotes the postfix form of the increment or decrement operator is not commonly used to pass arguments. It usually contains the value 0. However, it can be used as follows:class Int
{
public:
    Int &operator++( int n );
private:
    int _i;
};Int& Int::operator++( int n )
{
    if( n != 0 )    // Handle case where an argument is passed.
        _i += n;
    else
        _i++;       // Handle case where no argument is passed.
    return *this;
}
...Int i;
i.operator++( 25 ); // Increment by 25.There is no syntax for using the increment or decrement operators to pass these values other than explicit invocation, as shown in the preceding code. A more straightforward way to implement this functionality is to overload the addition/assignment operator (+=).

解决方案 »

  1.   

    在头文件中声明:
    type& operator++();    //前缀
    type operator++(type);  //后缀实现文件中定义:
    type& operator++(){
        ......
        return *this;
    }
    type operator(++type){
        type copy(*this);
        ......
        return copy;
    }
      

  2.   

    那么,这是定义的prefix和postfix是否还有原来的++运算符的那种特性呢?就是prefix可以先加1而postfix可以后加1?还望指教
      

  3.   

    我的意思是说,当我myclass++的时候会后加1,当我++myclass的时候会先加1,还能吗?
      

  4.   

    例子:class show{
         int x;
    public:
    show(int i=0){x=i;}int operator ++(){ return ++x;}//int 参数放在这里仅仅代表调用改函数是运算伏营放在操作数的后面,参数本身没有用处
    int operator ++(int){ return x++;}int GetX(return x;}};void main(void)
    {
        show demo(5);
        int i=demo++;
        cout<<i<<endl<<demo.Getx();
        int j=++demo;
        cout<<j<<endl<<demo.Getx();}5
    6
    7
    7