What is the point of constant class members?

 

I'm talking about const members of simple types that are not static (and are not pointers).

class CFoo
  {
private:
   const int i;
public:
   CFoo()
     {
      i = 1; // 'i' - constant cannot be modified
     }
  };

Why is it possible to make members of simple types const?

Such a member will not be initialized, that is, it will contain garbage. What is the point of protecting garbage?

 
Vladislav Boyko:
and are not pointers

Although, we can deprive the pointer of its meaning too

class CFoo
  {
   CFoo*const ptr;
  };

Can such members be used in any way?


I suspect that such checks simply aren't worth the extra complexity to the compiler.

In addition, I saw that in C++ you can do something like this:

class CFoo
  {
   int i = 1;
  };

Therefore, perhaps this would be useful for compatibility when adding new language features.

But these are just my guesses.

 

I found a way to use it😄

Sorry for the flood.

class CFoo
  {
public:
   const int m_a;
   const int m_b;
             CFoo(int a, int b): m_a(a), m_b(b) {};
  };

void OnStart()
  {
   CFoo obj(1,2);
  }
Reason: