For example, I have the following code.

#include <iostream> using namespace std; class X { int x; public: X(int i = 2): x {i} {} void print() const {cout << x;} // ^^^^^ int getx() const {return x;} // ^^^^^ X operator-(const X& rop) {return x + 2*rop.x;} // ^^^^^ int operator-(int rop) {return 2*x + rop;} }; int operator-(int lop, const X& rop) { // ^^^^^ return lop – rop.getx(); } int main() { X x {6},y; cout << x – 3 – y; return 0; } 

Tell me, please, what is the purpose of the keyword const in the function signature?

2 answers 2

That this code could work with constant objects. This const means that calling the print() method will not change the state of the object.

 void print() const {cout << x;} int getx() const {return x;} 

Here you don’t change the class object at all - so there is no point in doing NOT const (my opinion is to do everything possible with const , using it by default, and not using it only where it is needed), but these methods it will be possible to call, for example, when passing an object as a constant parameter - here’s how:

 X operator-(const X& rop) {return x + 2*rop.x;} 

You do not change the rop , so you can now call the operator for any object, including a temporary one.

By the way, I would also make the operators constant, like

 X operator-(const X& rop) const {return x + 2*rop.x;} 

because they do not change the state of the object.

  • Binary operators are generally better to do free functions. - αλεχολυτ
  • @Harry explain the meaning of the words. Здесь вы никак не меняете объект класса - поэтому нет никакого смысла делать НЕ const - perfect
  • @perfect const always set, if there is no reason not to put it - Vladimir Gamalyan
  • one
    @perfect Exactly - const set by default. Roughly speaking, it even seems to me that it would be more logical to introduce the nonconst modifier :) - Harry
  • @Harry. it just seems sensible to me to write. Здесь вы никак не меняете объект класса - поэтому делаем const . but it НЕ const me into a stupor) - perfect

The non-static member function, in addition to the parameters explicitly indicated in parentheses, also takes an implicit pointer this . Those. a pointer to an instance of the object on which this function was called.

If const not specified after the parameter list, this is of type X* const , i.e. pointer, which can not be changed, but the object itself can be changed through this pointer.

But when const present, the type of this becomes const X* const , i.e. a ban on attempts to change an object through this pointer is added.

The presence of const for a particular parameter of a function indicates that it is not this parameter that can be changed. For other parameters, their own limitations will apply.

In general, adding const where the object is not supposed to change allows you to write more reliable and understandable code, i.e. You can not accidentally change the object, which according to the logic of the developer should not change. If you try to change such an object, the compiler will generate an error.

On the topic of const we can remind about the existence of the mutable keyword, whose presence in the member of the object allows changing this member even if the function of the object was called const .