Different ways to initialize variables
Asked Answered
H

5

8

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;

?

How answered 20/3, 2014 at 20:3 Comment(3)
For ints it does the exact same thing (Assuming you meant int z(3) ). For more complex types read about constructors, operator= and other fun things.Petroleum
I think your second snippet was meant to be int z(3) which is equivalent.Lunde
Just to emphasize - if you're serious about learning C++ you should definitely learn about how assignment works, how construction works and the difference in some cases.Petroleum
B
8

You can use:

int z;
z = 3;

Or just:

int z = 3;

Or:

int z(3);

Or:

int z = int(3);
Beefsteak answered 20/3, 2014 at 20:6 Comment(3)
With C++11, you can also use int z={3} or int z{3}Leishmaniasis
The last example of the answer should be 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
int z {}; will initialize variable z to 0. that's the beauty of C++ 11. Where int z; will be initialized with garbage valueHonest
A
4

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.

Araby answered 20/3, 2014 at 20:5 Comment(0)
H
3

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

Hagiographer answered 4/8, 2016 at 20:22 Comment(0)
P
0

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 (=)

Ptosis answered 20/3, 2014 at 20:6 Comment(3)
int z(3) doesn't compile in CAraby
Ah in C it doesn't :).. that's my problemAraby
@staticx it's quite common syntax in C++, especially for more complex types - for example it's very common to see 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
H
0

As AndersK suggested, the best way to initialize variables in C++ is using {}. Because it ensures type safety, let's talk more about it.

Type Safety

Every object in C++ is used according to its type and an object needs to be initialized before its use.

Safe Initialization: {}

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.

Unsafe Initialization: = or ()

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

Hardball answered 24/12, 2022 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.