Operator Overloading in struct

后端 未结 4 1620
無奈伤痛
無奈伤痛 2021-02-07 11:39

Suppose I define this structure:

struct Point {
   double x, y;
};

How can I overload the + operator so that, declared,

         


        
4条回答
  •  猫巷女王i
    2021-02-07 12:06

    This will also work:

    struct Point{
        double x,y;
        Point& operator+(const Point& rhs){ 
                x += rhs.x;
                y += rhs.y;
                return *this;
        }
    }
    

提交回复
热议问题