后置++ 运算符重载
Coordinate.h
-----------------------------------------------------------------------------
#ifndef COORDINATE_H
#define COORDINATE_H
class Coordinate
{
//friend Coordinate &operator-(Coordinate &Coor); //友元一重运算符重载
public:
Coordinate(int x = 0, int y = 0);
Coordinate& operator-(); //成员函数一重运算符重载
/* 前置/后置 ++ */
Coordinate& operator++(); //前置++
Coordinate operator++(int); //后置++
int getX();
int getY();
private:
int m_iX;
int m_iY;
};
#endif
Coordinate.cpp
--------------------------------------------------------------------------
#include "Coordinate.h"
Coordinate::Coordinate(int x,int y)
{
m_iX = x;
m_iY = y;
}
Coordinate & Coordinate::operator-()
{
m_iX = -m_iX;
m_iY = -m_iY;
return *this;
}
Coordinate & Coordinate::operator++()
{
m_iX++;
m_iY++;
return *this;
}
Coordinate Coordinate::operator++(int)
{
Coordinate Old(*this);
m_iX++;
m_iY++;
return Old;
}
int Coordinate::getX()
{
return m_iX;
}
int Coordinate::getY()
{
return m_iY;
}
main.cpp
--------------------------------------------------------------------------
#include <iostream>
#include "Coordinate.h"
using namespace std;
int main()
{
Coordinate *coor = new Coordinate(1, 3);
cout<< coor->getX()<<","<<coor->getY()<<endl;
cout << ((*coor)++).getX() << ","
<< ((*coor)++).getY() << endl;
delete coor;
coor = NULL;
system("pause");
return 0;
}
----------------------------------------------------------
问题 当后置++ 输出的结果是 2,3 。正好和1,4相反
求解惑