+ 1

Norm and Negate using class

https://code.sololearn.com/cA193a9a14A1 The code works fine.I just want to know when to use const with the function and when not to.It is making me confused,

3rd May 2021, 9:08 PM
Ramisa Fariha
Ramisa Fariha - avatar
2 Réponses
+ 2
A const method is a method that doesn't mutate state of "this" or the instance of the class. You wouldn't make "negate" const because negate changes the values of x and y. In other words, a const method works even if "this" is a const. You could make const Point p1 in main and it'll compile since you don't call negate on p1. If you declare p2 as "const Point p2", you'll have a problem, though, since p2.negate is called which would mutate p2 and violate the meaning of "const". On a side note, I would rename your "norm" function to magnitude since "norm" could be confused with normalize or normal. normalize usually means scaling the vector such that magnitude becomes 1 and you're not doing that. The normal is another vector but you're returning a scalar/double.
3rd May 2021, 9:25 PM
Josh Greig
Josh Greig - avatar
0
@Josh thanks
3rd May 2021, 9:31 PM
Ramisa Fariha
Ramisa Fariha - avatar