C ++: "const" before class method

Taking for example the declaration of this method:

const Vector Vector::operator - ( const Vector& other ) const; 

I know that the second const makes the vector passed as an argument unchanged and that the last const declares that the method does not change the current instance of the Vector class ....

  • But what does the first or <mean? > first const ?
+4
source share
3 answers

This is an outdated security measure to prevent compromise code such as a - b = c .

(I say "deprecated" because it prevents semantics from moving that only works with non-constant values.)

+8
source

The first const means that this operator will return a constant vector object.

+4
source

This means that the return value is const Vector . This is more important in such cases: const int& Vector::get(int index) const;

0
source

Source: https://habr.com/ru/post/1340551/


All Articles