0
C++ Variable question
What does it mean when there are curly braces right after a variable name? Like this: int a{12} I've also seen something like this: int a{{12}} What are these?
5 Réponses
+ 4
There is no difference in this case, they all initialize a with 12.
It's just what you prefer.
However you should be aware that they cannot be interchanged everywhere.
For example
std::vector<int> v(10); // Creates a vector with 10 elements
std::vector<int> v{10}; // Creates a vector with 1 element
int a{}; // a = 0
int a(); // a is a function declaration
So yea, learn about the pitfalls of each of them.
Personally I prefer the =.
+ 2
https://www.geeksforgeeks.org/uniform-initialization-in-c/
Also the second example is illegal in this case.
std::array<int,10> a{{12}}; is allowed here though.
+ 2
Exclude the last one. Brain was on auto pilot. Lol
+ 1
interesting
So what's the difference between:
int a=12
int a(12)
int a{12}?