Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why put a "const" at the end? [duplicate]

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

like image 829
user658266 Avatar asked Mar 18 '11 03:03

user658266


People also ask

What is a const function?

ActiveOldestVotes 1074 A "const function", denoted with the keyword constafter a function declaration, makes it a compiler error for this class function to change a member variable of the class. However, reading of a class variables is okay inside of the function, but writing inside of this function will generate a compiler error.

What does const mean in C++?

A "const function", denoted with the keyword const after a function declaration, makes it a compiler error for this class function to change a member variable of the class. However, reading of a class variables is okay inside of the function, but writing inside of this function will generate a compiler error.

What happens when a function is declared as a const object?

When a function is declared as const, it can be called on any type of object, const object as well as non-const objects. Whenever an object is declared as const, it needs to be initialized at the time of declaration. however, the object initialization while declaring is possible only with the help of constructors.

Why do we initialise a constant immediately in a constructor?

One has to initialise it immediately in the constructor because, of course, one cannot set the value later as that would be altering it. For example, const int Constant1=96; will create an integer constant, unimaginatively called ‘Constant1’, with the value 96.


2 Answers

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.

like image 183
Maxpm Avatar answered Oct 24 '22 03:10

Maxpm


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 . . .
like image 38
Tamer Shlash Avatar answered Oct 24 '22 02:10

Tamer Shlash