I recently discovered that in C++ , we can initialize the integer , or for that matter other primitive data type in C++ using different ways.
The first one that most would be familiar is :
int i = 50; //initialised to 50
char c = 'a'; //initalised to 'a'
//And in similar way for all other data types
The one that I got to know recently is done as :
int i(50); //initialised to 50
char c('a'); //initalised to 'a'
Experimenting some more, I also found that the same thing can also be done in yet another way in C++ ! as follows :
int i{50}; //initialised to 50 just as before
char c{'a'}; //initalised to 'a'...same again
These are applicable to all other primitive data-types in C++ as well. Now I am can't help but wonder if the above different types of data initialisation are different in any possible way at all?
- Is one more efficient than other? Does using one over other have any benefits at all?
- Is there any scenario where one is preferable over other?
- If not, why all these different kinds of initialisation are provided? If yes, can you give some scenario where each has its place of use?