classPoint {int x_;int y_;public:Point(int x =0,int y =0);Point(constPoint&);~Point();Point&operator++(); //前置constPointoperator++(int); //后置,int仅仅为了重载Pointoperator+(constPoint&);Point&operator+=(constPoint&);voidDisplayPoint();};Point::Point(int x,int y){ x_ = x; y_ = y; cout <<"this is constructor"<< endl;}Point::Point(const Point& b){this->x_ =b.x_;this->y_ =b.y_; cout <<"this is copy constructor"<< endl;}Point::~Point(){ cout <<"this is destructor"<< endl;}Point& Point::operator+=(constPoint& _right){this->x_ +=_right.x_;this->y_ +=_right.y_;return*this;}Point Point::operator+(constPoint& _right){ Point temp;temp.x_ =this->x_ +_right.x_;temp.y_ =this->y_ +_right.y_;return temp;}Point& Point::operator++(){++x_;++y_;return*this;}constPoint Point::operator++(int){ Point temp(*this);this->x_++;this->y_++;return temp;}void Point::DisplayPoint(){ cout <<"x: "<<this->x_ << endl; cout <<"y: "<<this->y_ << endl;}
intmain() { Point a(1,1); cout << endl <<"this is a++: "<< endl; a++; cout << endl <<"this is ++a: "<< endl;++a;return0;}// 输出this is constructorthis is a++:this is copy constructorthis is copy constructorthis is destructorthis is destructorthis is ++a:this is destructor
a++将会有两次的拷贝构造与析构的调用,效率非常低。
classPoint {int x_;int y_;public:Point(int x =0,int y =0);Point(constPoint&);~Point();Point&operator++(); //前置Pointoperator++(int); //后置,去掉了const限定Pointoperator+(constPoint&);Point&operator+=(constPoint&);voidDisplayPoint();};Point::Point(int x,int y){ x_ = x; y_ = y; cout <<"this is constructor"<< endl;}Point::Point(const Point& b){this->x_ =b.x_;this->y_ =b.y_; cout <<"this is copy constructor"<< endl;}Point::~Point(){ cout <<"this is destructor"<< endl;}Point& Point::operator+=(constPoint& _right){this->x_ +=_right.x_;this->y_ +=_right.y_;return*this;}Point Point::operator+(constPoint& _right){ Point temp;temp.x_ =this->x_ +_right.x_;temp.y_ =this->y_ +_right.y_;return temp;}Point& Point::operator++(){++x_;++y_;return*this;}Point Point::operator++(int) // int仅仅为了重载{ Point temp(*this);this->x_++;this->y_++;return temp;}void Point::DisplayPoint(){ cout <<"x: "<<this->x_ << endl; cout <<"y: "<<this->y_ << endl;}intmain() { Point b(2,2); cout << endl <<"this is &b: "<<&b << endl; Point* c; c =&(++b); cout << endl <<"this is c = &(++b): "<< c << endl;c->DisplayPoint(); c =&(b++); cout << endl <<"this is c = &(b++): "<< c << endl;c->DisplayPoint();++b =*c;b.DisplayPoint(); b++=*c;b.DisplayPoint();return0;}// 输出this is constructorthis is &b: 008FFD38this is c =&(++b): 008FFD38x: 3y: 3this is copy constructorthis is copy constructorthis is destructorthis is destructorthis is c =&(b++): 008FFC5Cx: 3y: 3x: 3y: 3this is copy constructorthis is copy constructorthis is destructorthis is destructorx: 4y: 4this is destructor
可以看到++b返回的对象指针跟b原来的地址是一样的,而b++返回的对象地址跟原来的b地址不一样(应该是临时对象的地址),虽然可以取到地址,但当成左值将有可能导致错误。比如b++ = c就不会将c给b,达不到原来的目的。为此,我们应该将后置的++函数返回值定义为const类型:const Point Point::operator++(int),就可以避免这种当成左值情况出现。另外发现返回temp的引用可以减少一次拷贝和析构,但是不建议返回局部变量的引用!因为函数退出,局部变量将析构,引用就会指向不确定内存。