📜 ⬆️ ⬇️

Many faces const

The const keyword is one of the most significant in C ++. Correct use of const allows you to organize many checks at the compilation stage and avoid many errors from among those that can be difficult to find using debuggers and / or code analysis.

The first half of the note is intended more for beginners (I hope the mnemonic rule will help you remember where and why const is used), but perhaps experienced programmers will be able to gather interesting information about overloading const methods.

Constants and data


The simplest case is constant data. There are several possible recording options:
')
  const int i (1);
 int const j (1);
 int const k = 1; 

All of them are correct and do the same thing - they create a variable, the value of which cannot be changed.

  const int k = 1;
 k = 7;  // <- error at compile time! 

Constants and pointers


When using const with pointers, the effect of the modifier can extend either to the value of the pointer, or to the data pointed to by the pointer.

Works (const refers to data):

  const char * a = "a";
 a = "b"; 

The same also works:

  char const * a = "a";
 a = "b"; 

But this does not work:

  char * const a = "a";
 a = "b";  // <- does not work 

If the assignment operation would change not the pointer, but the data:

  * a = 'Y'; 

then the situation would be diametrically opposite.

There is a mnemonic rule that makes it easy to remember what const belongs to. It is necessary to draw a line through the "*", if const to the left, then it refers to the value of the data; if to the right, to the value of the pointer.

And of course, const can be written twice:

  const char * const s = "data"; 

Constants and arguments / function results


With functions, the word const is used according to the same rules as in the description of ordinary data.

Constants and methods (overload)


But with the methods there is one subtlety.

First, for methods it is admissible to use const, applied to this. The syntax is:

  class A {
 private:
   int x;
 public:
   void f (int a) const {
     x = a;  // <- does not work
   }
 }; 

In addition, this const allows you to overload methods. Thus, you can write optimized variants of methods for constant objects.

I explain:

  class A {
 private:
   int x;
 public:
   A (int a) {
     x = a;
     cout << "A (int) // x =" << x << endl;
   }
   void f () {
     cout << "f () // x =" << x << endl;
   }
   void f () const {
     cout << "f () const // x =" << x << endl;
   }
 };
 int main () {
   A a1 (1);
   a1.f ();
   A const a2 (2);
   a2.f ();
   return 0;
 } 

Result:

  A (int) // x = 1
 f () // x = 1
 A (int) // x = 2
 f () const // x = 2 

That is, for a constant object (with x = 2), the corresponding method was called.

It remains only to add that if you plan to use const-objects, then you need to implement const-methods. If you do not implement non-const methods in this case, then const methods will be silently used in all cases. In short, const is better to use where it is possible.



The picture is taken from here http://museum.dp.ua/affiliate/hmuseum/idols.html

And yet ... I was going on vacation ... maybe I can not respond to comments until Monday-Tuesday. Do not think for inattention :-)

Source: https://habr.com/ru/post/59558/


All Articles