Yukon
Yukon

Reputation: 833

C++: "const" in front of a class method

Taking for example this method declaration:

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

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

Upvotes: 6

Views: 1536

Answers (3)

Wheatevo
Wheatevo

Reputation: 643

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

Upvotes: 4

fredoverflow
fredoverflow

Reputation: 263360

It is an outdated security measure to prevent nonsense code like a - b = c to compile.

(I say "outdated" because it prevents move semantics which only works with non-const rvalues.)

Upvotes: 9

Shiroko
Shiroko

Reputation: 1427

It means the return value is a const Vector. It has more meaning in cases such as this: const int& Vector::get(int index) const;

Upvotes: 0

Related Questions