I am learning C++
.
I've seen some people doing this:
int a = 2;
But also:
int b(2);
And also:
int c{2};
What am I supposed to use? What are the differences between those?
Thanks, Do'
I am learning C++
.
I've seen some people doing this:
int a = 2;
But also:
int b(2);
And also:
int c{2};
What am I supposed to use? What are the differences between those?
Thanks, Do'
What am I supposed to use ?
You can use any of them.
What are the differences between those ?
int a = 2; // A int b(2); // B int c{2}; // C
A is copy initialisation. B and C are direct initialisation.
C uses a braced-init-list and is therefore direct list initialisation. A and B allow initialising with a narrowing conversion while list initialisation allows only non-narrowing conversion. This is sometimes advantageous, since it can help detect unintentional narrowing which is a source of bugs. In this particular case however, it is safe to assume that 2 is appropriate value for int
.