What Is the Meaning of a Const At End of a Member Function

What is the meaning of a const at end of a member function?

It means that *this is const inside that member function, i.e. it doesn't alter the object.

The keyword this is a prvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*. [section 9.3.2 §1]

In a const member function, the object for which the function is called is accessed through a const access path; therefore, a const member function shall not modify the object and its non-static data members. [section 9.3.2 §2]

This means that a const member function can be called on a const instance of the class. A non-const member function can't be called on [1]a const object, since it could potentially try to modify it.


[1] Note: a temporary is not a const object unless it's of const type.

What does const mean (in context) at the end of a function definition?

It means that this function does not modify the observable state of an object.

In compiler terms, it means that you cannot call a function on a const object (or const reference or const pointer) unless that function is also declared to be const. Also, methods which are declared const are not allowed to call methods which are not.

Update: as Aasmund totally correctly adds, const methods are allowed to change the value of members declared to be mutable.

For example, it might make sense to have a read-only operation (e.g. int CalculateSomeValue() const) which caches its results because it's expensive to call. In this case, you need to have a mutable member to write the cached results to.

I apologize for the omission, I was trying to be fast and to the point. :)

What is meant with const at end of function declaration?

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.

Another way of thinking about such "const function" is by viewing a class function as a normal function taking an implicit this pointer. So a method int Foo::Bar(int random_arg) (without the const at the end) results in a function like int Foo_Bar(Foo* this, int random_arg), and a call such as Foo f; f.Bar(4) will internally correspond to something like Foo f; Foo_Bar(&f, 4). Now adding the const at the end (int Foo::Bar(int random_arg) const) can then be understood as a declaration with a const this pointer: int Foo_Bar(const Foo* this, int random_arg). Since the type of this in such case is const, no modifications of member variables are possible.

It is possible to loosen the "const function" restriction of not allowing the function to write to any variable of a class. To allow some of the variables to be writable even when the function is marked as a "const function", these class variables are marked with the keyword mutable. Thus, if a class variable is marked as mutable, and a "const function" writes to this variable then the code will compile cleanly and the variable is possible to change. (C++11)

As usual when dealing with the const keyword, changing the location of the const key word in a C++ statement has entirely different meanings. The above usage of const only applies when adding const to the end of the function declaration after the parenthesis.

const is a highly overused qualifier in C++: the syntax and ordering is often not straightforward in combination with pointers. Some readings about const correctness and the const keyword:

Const correctness

The C++ 'const' Declaration: Why & How

Why we need to put const at end of function header but static at first?

with a const instance method such as int get_hours() const;, the const means that the definition of int get_hours() const; will not modify this.

with a static method such as static void fun();, const does not apply because this is not available.

you can access a static method from the class or instance because of its visibility. more specifically, you cannot call instance methods or access instance variables (e.g. x, hours) from the static method because there is not an instance.

class t_classname {
public:
static void S() { this->x = 1; } // << error. this is not available in static method
void s() { this->x = 1; } // << ok
void t() const { this->x = 1; } // << error. cannot change state in const method
static void U() { t_classname a; a.x = 1; } // << ok to create an instance and use it in a static method
void v() const { S(); U(); } // << ok. static method is visible to this and does not mutate this.

private:
int a;
};

why put a const at the end?

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.

Why using the const keyword before and after method or function name?

const T& get_data() const { return data_; }
^^^^^

means it will return a const reference to T (here data_)

Class c;
T& t = c.get_data() // Not allowed.
const T& tc = c.get_data() // OK.




const T& get_data() const { return data_; }
^^^^^

means the method will not modify any member variables of the class (unless the member is mutable).

void Class::get_data() const {
this->data_ = ...; // is not allowed here since get_data() is const (unless 'data_' is mutable)
this->anything = ... // Not allowed unless the thing is 'mutable'
}

C++ Difference between const positioning

const int MyClass::showName(string id) returns a const int object. So the calling code can not change the returned int. If the calling code is like const int a = m.showName("id"); a = 10; then it will be marked as a compiler error. However, as noted by @David Heffernan below, since the integer is returned by copy the calling code is not obliged to use const int. It can very well declare int as the return type and modify it. Since the object is returned by copy, it doesn't make much sense to declare the return type as const int.

int MyClass::showName(string id) const tells that the method showName is a const member function. A const member function is the one which does not modify any member variables of the class (unless they are marked as mutable). So if you have member variable int m_a in class MyClass and if you try to do m_a = 10; inside showName you will get a compiler error.

Third is the combination of the above two cases.



Related Topics



Leave a reply



Submit