2070 / C++ / Класи / Перевантаження операторів / Перевантаження оператору присвоювання =

 

* Стандартне перевантаження = компілятор Visual Studio робить автоматично

#include <iostream>
using namespace std;

class Point
{
  public:
    int x, y;
    Point& operator=(const Point&);
};

Point& Point::operator=(const Point& otherPoint)
{
  x = otherPoint.x + 7;
  y = otherPoint.y + 7;
  return *this;
}

int main()
{
  Point pt1, pt2;
  pt1.x = 1;
  pt1.y = 2;
  pt2 = pt1;
  cout << pt2.x << endl;
  cout << pt2.y << endl << endl;
  system("pause");
  return 0;
}

8
9