一元运算符重载 后置++
为什么我 cout<<(coor1++).getX(); 输出的是-858993460, 但是这样 coor++; cout<<coor1.getX(); 输出的就是2.
为什么我 cout<<(coor1++).getX(); 输出的是-858993460, 但是这样 coor++; cout<<coor1.getX(); 输出的就是2.
2016-10-25
主函数
#include <iostream>
#include "Coordinate.h"
using namespace std;
int main()
{
Coordinate coor1(1, 3);
cout << coor1.getX()<<","<<coor1.getY() << endl;
//cout << (coor1++).getX();这样的输出是-858993460
coor1++;
cout << coor1.getX() << "," << coor1.getY() << endl;
system("pause");
return 0;
}
定义
#include "Coordinate.h"
Coordinate::Coordinate(int x,int y)
{
m_iX = x;
m_iY = y;
}
int Coordinate::getX()
{
return m_iX;
}
int Coordinate::getY()
{
return m_iY;
}
Coordinate &Coordinate::operator++(int)
{
Coordinate old(*this);
this->m_iX++;
this->m_iY++;
return old;
}
声明
#include <iostream>
using namespace std;
class Coordinate
{
public:
Coordinate(int ,int);
Coordinate &operator++(int);
int getX();
int getY();
private:
int m_iX;
int m_iY;
};
举报