6

Possible Duplicates:
c++ const use in class methods
Meaning of “const” last in a C++ method declaration?

int operator==(const AAA &rhs) const;

This is a operator overloading declaration. Why put const at the end? Thanks

Community
  • 1
  • 1
user658266
  • 2,397
  • 4
  • 20
  • 14
  • 4
    possible duplicate of [c++ const use in class methods](http://stackoverflow.com/questions/5346695/c-const-use-in-class-methods) (and numerous others!) – johnsyweb Mar 18 '11 at 03:30

5 Answers5

10

The const keyword signifies that the method isn't going to change the object. Since operator== is for comparison, nothing needs to be changed. Therefore, it is const. It has to be omitted for methods like operator= that DO modify the object.

It lets the compiler double-check your work to make sure you're not doing anything you're not supposed to be doing. For more information check out http://www.parashift.com/c++-faq-lite/const-correctness.html.

Maxpm
  • 24,113
  • 33
  • 111
  • 170
  • one more stupid question, the "const" in "()" is already guarantee that object won't be changed. Is the last "const" duplicated? – user658266 Mar 18 '11 at 03:41
  • 4
    @user658266 - `const` at the end has a different meaning than `const` in `()`. `const` at the end signifies that the object upon which the method is called is not modified. While `const` in `()` signifies that the received argument is not modified. – Mahesh Mar 18 '11 at 03:45
  • 2
    @user658266: "const" in "()" guarantees that the argument being passed as an parameter(in this case rhs) to the overloaded operator "=" doesn't get modified. The "const" at the end of the function indicates that the function doesn't change any members of the object on which it is being called. – Alok Save Mar 18 '11 at 03:46
  • 2
    @user658266 To clarify: `void MyFunction(const int MyInt) const;` is a member function that takes an **integer that cannot be changed** and **will not modify the class it is a part of.** – Maxpm Mar 18 '11 at 03:46
4

Making a method const will let a contant object of the class to call it. because this method cannot change any of the object's members (compiler error).

It may deserve mention that const is a part of the method's signature, so in the same class you may have two methods of the same prototype but one is const and the other isn't. In this case, if you call the overloaded method from a variable object then the non-const method is called, and if you call it from a constant object then the const method is called.

However, if you have only a const method (there is no non-const overload of it), then it's called from both variable and constant object.

For Example :

#include <iostream>

using std::cout;

class Foo
{
public:
    bool Happy;
    Foo(): Happy(false)
    {
        // nothing
    }
    void Method() const
    {
        // nothing
    }
    void Method()
    {
        Happy = true;
    }
};

int main()
{
    Foo A;
    const Foo B;
    A.Method();
    cout << A.Happy << '\n';
    B.Method();
    cout << B.Happy << '\n';
    return 0;
}

Will output :

1
0
Press any key to continue . . .
Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
1

"const" at the end of any c++ declaration tells the compiler that it won't change the thing it belongs to.

Rob Agar
  • 12,337
  • 5
  • 48
  • 63
1

That marks the method itself as being constant, which means the compiler will permit you to use the method whenever you have a const reference to the relevant object. If you have a const reference, you otherwise can only invoke methods that are also declared as const.

DuckMaestro
  • 15,232
  • 11
  • 67
  • 85
1

A 'const' at the end of a method declaration marks that method as being safe to call on a constant object.

capveg
  • 107
  • 8