6

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....

  • But what exactly does the first const mean or lead to?
Yukon
  • 783
  • 1
  • 6
  • 5
  • You should really call them "member functions" instead of "class methods". But that's just my pedantic opinion. – rubenvb Feb 20 '11 at 21:13
  • @rubenvb: why? Doesn't "method" mean "member function" (in this context)? – Beta Feb 20 '11 at 21:17
  • @Beta: well, here's my reasoning: 1) "method" isn't used in the Standard anywhere, 2) It's fo Java-like (I know, personal `:s`), and 3) What would a "non-class method" be? A plain function, which would make a function of a class a member function, as in data member. (just forget number 2 if you don't agree, it doesn't add to my point...) – rubenvb Feb 20 '11 at 21:34
  • @rubenvb: interesting... 1) I respect the Standard, 2) I don't know Java well enough to notice Java-sounding terms (or hate it enough to avoid them), 3) I consider "non-class method" an oxymoron. – Beta Feb 20 '11 at 21:48

3 Answers3

9

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.)

Community
  • 1
  • 1
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • 1
    Does it?(prevent move semantics) All my [tests](http://ideone.com/3pqH5) indicate otherwise. Am I missing something? – Benjamin Lindley Feb 20 '11 at 21:28
  • @Pig: Please disable constructor elision. Then you will see a move followed by a copy. If you remove the `const`, you will see two moves instead. – fredoverflow Feb 20 '11 at 21:35
  • First time I read this too... (const preventing move semantics). – rubenvb Feb 20 '11 at 21:35
  • @ruben: It is definitely preventing move semantics, because `Foo&&` does not bind to `const Foo`. – fredoverflow Feb 20 '11 at 21:36
  • Scott Meyers discusses the issue [here](http://www.aristeia.com/BookErrata/mec++-errata.html) (search for "values const"). – fredoverflow Feb 20 '11 at 21:40
  • @Fred: Elision is disabled by the if statement. If elision were occuring, I should see a single default construction. Instead, I am seeing a default, and a move. Maybe you are right, in which case my compiler(VC++) as well as gcc4.5(which ideone is using) are performing optimizations which they shouldn't be allowed to do. – Benjamin Lindley Feb 20 '11 at 22:02
  • @Pig: The object denoted to by `x` inside `f` is indeed successfully moved into the object denoted to by `f(true)`, because `x` is not const. But then the compiler must do a copy to initialize the `x` inside `main`, because `f(true)` *is* `const`. Any compiler worth its salt will optimize that copy away, of course. Compile under g++ with `-fno-elide-constructors` and you will see the copy. Remove the `const` from the return type, and you will see another move instead. – fredoverflow Feb 20 '11 at 22:20
4

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

Wheatevo
  • 643
  • 1
  • 5
  • 10
  • Is this useful for operator overloading in a mathematical vector object? – Yukon Feb 20 '11 at 21:07
  • Nope, it is not necessary. `a = b - c - d` would work just fine if you returned a non-const Vector. – fredoverflow Feb 20 '11 at 21:10
  • Ah, thanks for that. I've edited my answer for correctness. :X – Wheatevo Feb 20 '11 at 21:11
  • It is not necessary for chaning, but it stops you from assigning to the result of the operation. a - b = c - d will be a compile time error. Some people believe this is important. – Bo Persson Feb 20 '11 at 21:13
0

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;

Shiroko
  • 1,437
  • 8
  • 12