There are different ways to initialize a variable in c++.
int z(3)
is same as int z=3
.
Is
int z;
z(3);
same as
int z;
z=3;
?
There are different ways to initialize a variable in c++.
int z(3)
is same as int z=3
.
Is
int z;
z(3);
same as
int z;
z=3;
?
You can use:
int z;
z = 3;
Or just:
int z = 3;
Or:
int z(3);
Or:
int z = int(3);
int z={3}
or int z{3}
–
Leishmaniasis auto z = int(3);
, since the type is stated explicitly at the right side. However, especially for non-primitive types, direct initialization as shown in the second example by @Leishmaniasis is the most flexible variant (especially when refactoring). –
Amputate z(3);
by itself is invalid syntax. The only way it would be valid is if you had a function called z
, that you passed in the integer 3. But even that wouldn't necessarily set the local variable z
to 3.
int z(3);
will compile in C++ only (not C).
int z;
z=3;
is valid syntax and will set z
equal to 3.
Both int z(3)
and int z; z=3;
are the same in this case.
No z(3);
is calling a function called z
with argument 3
If you want to initialize using that syntax you need to do that in the declaration
int z(3):
You can however initialize z like this
int z{3};
This is the recommended way to initialize variables since C++11
The advantage with syntax is that it gives an error when there is a narrowing conversion i.e. if it is not guaranteed that the value will fit into the target variable you get a compile error. The syntax can be used on any object.
Reference: Item 7, Effective Modern C++ by Scott Meyers
No, the first option is incorrect: most vexing parse. The compiler thinks that z
is a function. However these too are basically exactly the same:
int z(3);
int z = 3;
Note that they are the same in this case, but on objects, the first calls a matching constructor while the second requires an overload of copy assignment operator (=
)
int z(3)
doesn't compile in C –
Araby std::vector<MyType> vec(30)
and such. awesomeyi, I would appreciate it if you elaborate on the fact they're not always the same but are the same in this case. –
Petroleum As AndersK suggested, the best way to initialize variables in C++ is using {}. Because it ensures type safety, let's talk more about it.
Every object in C++ is used according to its type and an object needs to be initialized before its use.
The compiler protects from information loss during type conversion. For example,
int a{7};
The initialization is OK
int b{7.5}
Compiler shows ERROR because of information loss.
The compiler doesn't protect from information loss during type conversion.
int a = 7
The initialization is OK
int a = 7.5
The initialization is OK, but information loss occurs. The actual value of a will become 7.0
int c(7)
The initialization is OK
int c(7.5)
The initialization is OK, but information loss occurs. The actual value of a will become 7.0
© 2022 - 2024 — McMap. All rights reserved.
int z(3)
). For more complex types read about constructors,operator=
and other fun things. – Petroleumint z(3)
which is equivalent. – Lunde