Difference between declaring variables with () vs {}

I just realized I have been declaring variables with () while the instructor uses {}. This hasn’t been an issue until I used the parenthesis in a changeable bool. It gave me an error but when I changed it to the curly braces it worked.

I’m just curious about the difference between using () vs {} when declaring variables.

Braced initialization { } allows predefined variable types (int, float, bool, char, etc) to be initialized with default values. Using ( ) would attempt to call a constructor for that type, and in the case of these same predefined variable types, you must pass in a value into that constructor.

You’ll learn about constructors later in the course but to give a quick preview, constructors tell the compiler how to setup a user-defined variable.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms