Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do const variables have to be initialized right away?

This is a general programming question. I'm learning about C++ and I've learned that any const variables, ie: const int i, or int *const ptr, have to be initialized right away.

This is also the underlying reason that references to addresses must be initialized right away, because the addresses are const.

But I can't find the reason why this must be done / why this rule is imposed.

Can anyone explain this for me please?

like image 652
Lebowski156 Avatar asked Dec 09 '11 02:12

Lebowski156


People also ask

Why a constant must be initialized when it is declared?

When a variable is declared as const it means that , variable is read-only ,and cant be changed . so in order to make a variable read only it should be initialized at the time it is declared.

Do const variables need to be initialized?

A constant variable must be initialized at its declaration. To declare a constant variable in C++, the keyword const is written before the variable's data type.

Is it necessary to initialize the const variable in C?

In C, virtually no kind of object is ever required to be initialized, and has never been required to be initialized.

What happens when variables are not properly initialized?

Notice that a variable that is not initialized does not have a defined value, hence it cannot be used until it is assigned such a value.


2 Answers

Because there is no way you can initialize it, or assigned with a value, later on.

const int size; //no initialization (error)

size = 100; //error - you cannot assign a const variable.

Now if a variable which is neither having any meaningful value, nor are you allowed to make it to have value later on because it is a const variable, then what is the point of such a variable? It is completely useless.

However, this is true for only built-in and POD types:

struct A{}; //POD type
struct B{ B(){} }; //Non POD type because it has user-defined constructor!

const int i; //error - built-in type
const A a;   //error - POD type
const B b;   //ok -    Non POD type

//likewise
const std::string s; //ok - std::string is a non-POD
const std::vector<std::string> v; //ok - std::vector is a non-POD

Actually a NON-POD type cannot remain uninitialized, because the default constructor will be called, and the object would get initialized.


Now consider this struct,

struct C
{
   const int i;
   C() {}
};

C is definitely a non-POD type, because it has user-defined constructor. Also note that in the constructor, it doesn't initialize i which is int, declared as const. Because of this uninitialized const i, the following would give error:

const C c; //error - 

One might think the error is because of const in the above declaration of variable c. But that is short-sightedness and is not true. Even if you remove const, it would give error:

C c; //error - same error

The error is because of C::i which is declared const but has not been initialized.

Demo : http://ideone.com/NJT8L


This analysis also demonstrates that built-in types do not get initialized automatically even if they're members of non-POD types. This is true of non-POD class types as well.

And the syntax to default initialization for built-in types (and POD types) is this:

struct C
{
    const int i;
    C() : i() {} //note the syntax - it is called member-initialization list
};

Now this is allowed :

C x; //ok
const C y; //ok

Demo : http://ideone.com/84vD9


As for what makes a struct/class POD, see this topic:

  • Can't C++ POD type have any constructor?
like image 152
Nawaz Avatar answered Oct 21 '22 20:10

Nawaz


Because if you could assign to them later, they wouldn't be "const".

like image 37
Oliver Charlesworth Avatar answered Oct 21 '22 18:10

Oliver Charlesworth