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