16-variable-assignment-and-initialization
16-variable-assignment-and-initialization
In the previous lesson (1.3 -- Introduction to objects and variables), we covered how to define a variable that we can use to store values. In this lesson, we’ll
explore how to actually put values into variables and use those values.
As a reminder, here’s a short snippet that first allocates a single integer variable named x, then allocates two more integer variables named y and z:
Variable assignment
After a variable has been defined, you can give it a value (in a separate statement) using the = operator. This process is called assignment, and the = operator is
called the assignment operator.
By default, assignment copies the value on the right-hand side of the = operator to the variable on the left-hand side of the operator. This is called copy
assignment.
1 #include <iostream>
2 int main()
3 {
4 int width;
5 width = 5; // copy assignment of value 5 into variable
width
6
std::cout << width; // prints 5
7 return 0;
8 }
This prints:
57
When we assign value 7 to variable width, the value 5 that was there previously is overwritten. Normal variables can only hold one value at a time.
Warning
One of the most common mistakes that new programmers make is to confuse the assignment operator ( = ) with the equality operator ( == ). Assignment ( = )
is used to assign a value to a variable. Equality ( == ) is used to test whether two operands are equal in value.
Initialization
One downside of assignment is that it requires at least two statements: one to define the variable, and one to assign the value.
These two steps can be combined. When a variable is defined, you can also provide an initial value for the variable at the same time. This is called initialization.
The value used to initialize a variable is called an initializer.
You may see the above forms written with different spacing (e.g. int d{7}; ). Whether you use extra spaces for readability or not is a matter of personal
preference.
Default initialization
When no initialization value is provided (such as for variable a above), this is called default initialization. In most cases, default initialization leaves a variable
with an indeterminate value.
We’ll discuss this case further in lesson (1.6 -- Uninitialized variables and undefined behavior).
Copy initialization
When an initializer is provided after an equals sign, this is called copy initialization. This form of initialization was inherited from C.
Much like copy assignment, this copies the value on the right-hand side of the equals into the variable being created on the left-hand side. In the above snippet,
variable width will be initialized with value 5 .
Copy initialization had fallen out of favor in modern C++ due to being less efficient than other forms of initialization for some complex types. However, C++17
remedied the bulk of these issues, and copy initialization is now finding new advocates. You will also find it used in older code (especially code ported from C), or
by developers who simply think it looks more natural and is easier to read.
Direct initialization
When an initializer is provided inside parenthesis, this is called direct initialization.
Direct initialization was initially introduced to allow for more efficient initialization of complex objects (those with class types, which we’ll cover in a future chapter).
Just like copy initialization, direct initialization had fallen out of favor in modern C++, largely due to being superseded by list initialization. However, we now know
that list initialization has a few quirks of its own, and so direct initialization is once again finding use in certain cases.
One of the reasons direct initialization had fallen out of favor is because it makes it hard to differentiate variables from functions. For example:
List initialization
The modern way to initialize objects in C++ is to use a form of initialization that makes use of curly braces. This is called list initialization (or uniform initialization
or brace initialization).
As an aside…
Prior to the introduction of list initialization, some types of initialization required using copy initialization, and other types of initialization required using direct
initialization. List initialization was introduced to provide a more consistent initialization syntax (which is why it is sometimes called “uniform initialization”)
that works in most cases.
Additionally, list initialization provides a way to initialize objects with a list of values (which is why it is called “list initialization”). We show an example of this
in lesson 16.2 -- Introduction to std::vector and list constructors.
List initialization has an added benefit: “narrowing conversions” in list initialization are ill-formed. This means that if you try to brace initialize a variable using a
value that the variable can not safely hold, the compiler is required to produce a diagnostic (usually an error). For example:
1 int width { 4.5 }; // error: a number with a fractional value can't fit into an
int
In the above snippet, we’re trying to assign a number (4.5) that has a fractional part (the .5 part) to an integer variable (which can only hold numbers without
fractional parts).
Copy and direct initialization would simply drop the fractional part, resulting in the initialization of value 4 into variable width. Your compiler may optionally warn
you about this, since losing data is rarely desired. However, with list initialization, your compiler is required to generate a diagnostic in such cases.
Conversions that can be done without potential data loss are allowed.
To summarize, list initialization is generally preferred over the other initialization forms because it works in most cases, it disallows narrowing conversions, and it
supports initialization with lists of values (something we’ll cover in a future lesson). While you are learning, we recommend sticking with list initialization (or value
initialization).
Best practice
Prefer direct list initialization (or value initialization) for initializing your variables.
Author’s note
Bjarne Stroustrup (creator of C++) and Herb Sutter (C++ expert) also recommend using list initialization to initialize your variables.
In modern C++, there are some cases where list initialization does not work as expected. We cover one such case in lesson 16.2 -- Introduction to std::vector
and list constructors.
Because of such quirks, some experienced developers now advocate for using a mix of copy, direct, and list initialization, depending on the circumstance.
Once you are familiar enough with the language to understand the nuances of each initialization type and the reasoning behind such recommendations, you
can evaluate on your own whether you find these arguments persuasive.
Related content
For more discussion on this topic, Bjarne Stroustrup (creator of C++) and Herb Sutter (C++ expert) make this recommendation themselves here.
We explore what happens if you try to use a variable that doesn’t have a well-defined value in lesson 1.6 -- Uninitialized variables and undefined behavior.
Best practice
Initialize your variables upon creation.
1 int a,
b;
We also noted that best practice is to avoid this syntax altogether. However, since you may encounter other code that uses this style, it’s still useful to talk a little
bit more about it, if for no other reason than to reinforce some of the reasons you should be avoiding it.
Unfortunately, there’s a common pitfall here that can occur when the programmer mistakenly tries to initialize both variables by using one initialization statement:
int a = 5, b = 5; // correct
In the top statement, variable “a” will be left uninitialized, and the compiler may or may not complain. If it doesn’t, this is a great way to have your program
intermittently crash or produce sporadic results. We’ll talk more about what happens if you use uninitialized variables shortly.
The best way to remember that this is wrong is to consider the case of direct initialization or brace initialization:
1 int a, b( 5
);
int c, d{ 5
};
Because the parenthesis or braces are typically placed right next to the variable name, this makes it seem a little more clear that the value 5 is only being used to
initialize variable b and d, not a or c.
1 int main()
{
2 int x { 5 }; // variable
3 defined
return 0;
4 }
When compiling this with the g++ compiler, the following error is generated:
1. If the variable really is unused, then the easiest option is to remove the defintion of x (or comment it out). After all, if it’s not used, then removing it won’t
affect anything.
2. Another option is to simply use the variable somewhere:
1 #include <iostream>
2 int main()
3 {
4 int x { 5 };
5
std::cout << x; // variable now used
6 somewhere
7
return 0;
}
But this requires some effort to write code that uses it, and has the downside of potentially changing your program’s behavior.
In some cases, neither of the above options are desirable. Consider the case where we have a bunch of math/physics values that we use in many different programs:
1 int main()
{
2 double pi { 3.14159 };
3 double gravity { 9.8 };
double phi { 1.61803 };
If we use these a lot, we probably have these saved somewhere and copy/paste/import them all together.
However, in any program where we don’t use all of these values, the compiler will complain about each variable that isn’t actually used. While we could go through
and remove/comment out the unused ones for each program, this takes time and energy. And later if we need one that we’ve previously removed, we’ll have to go
back and re-add it.
To address such cases, C++17 introduced the [[maybe_unused]] attribute, which allows us to tell the compiler that we’re okay with a variable being unused.
The compiler will not generate unused variable warnings for such variables.
The following program should generate no warnings/errors:
1 int main()
2 {
3 [[maybe_unused]] double pi { 3.14159 };
[[maybe_unused]] double gravity { 9.8 };
[[maybe_unused]] double phi { 1.61803 };
return 0;
5 }
Additionally, the compiler will likely optimize these variables out of the program, so they have no performance impact.
Author’s note
In future lessons, we’ll often define variables we don’t use again, in order to demonstrate certain concepts. Making use of [[maybe_unused]] allows us
to do so without compilation warnings/errors.
Quiz time
Question #1
What is the difference between initialization and assignment?
Show Solution
Question #2
What form of initialization should you prefer when you want to initialize a variable with a specific value?
Show Solution
Question #3
What are default initialization and value initialization? What is the behavior of each? Which should you prefer?
Show Solution
Next lesson
1.5 Introduction to iostream: cout, cin, and endl
Back to table of contents
Previous lesson
1.3 Introduction to objects and variables