Ch2.5: Assignment
What Is Assignment?
In C++, assignment means giving an existing object a new value after it has already been initialized. Assignment is not initialization. Initialization happens when an object is created and given its first value. Assignment happens later, when you replace the value stored in that object.
Examples of Assignment
#include <fast_io.h>
#include <cstddef>
#include <cstdint>
int main()
{
using namespace ::fast_io::iomnp;
// Initialization
::std::size_t a{10}; // object a initialized to 10
println("a = ", a); // prints 10
// Assignment
a = 20; // assign new value to a
println("a = ", a); // prints 20
// Multiple assignments
::std::int_least32_t b{5}; // initialized to 5
println("b = ", b); // prints 5
b = 15; // assigned new value
println("b = ", b); // prints 15
b = b + 10; // assigned result of expression
println("b = ", b); // prints 25
// Boolean assignment
bool flag{false}; // initialized to false
println("flag = ", flag); // prints 0
flag = true; // assigned new value
println("flag = ", flag); // prints 1
// Assignment between objects
::std::size_t x{100};
::std::size_t y{200};
println("x = ", x, " y = ", y); // prints 100 200
x = y; // assign value of y to x
println("x = ", x, " y = ", y); // prints 200 200
}
Assignment vs Initialization
It is important to distinguish between initialization and assignment:
- Initialization gives an object its first value at the moment it is created.
- Assignment replaces the value of an object that already exists.
#include <fast_io.h>
int main()
{
using namespace ::fast_io::iomnp;
int i = 5; // initialization
println("i = ", i);
i = 10; // assignment
println("i = ", i);
}
Key takeaway: Initialization happens once, when the object is created. Assignment can happen many times during the lifetime of the object.
Lvalues and Rvalues
To understand assignment, we need to know the concepts of lvalue and rvalue:
- Lvalue (left value): An expression that refers to a memory location and can appear on the left-hand side of an assignment.
Examples: named objects like
x, dereferenced pointers like*p. - Rvalue (right value): A temporary value or literal that does not persist beyond the expression.
Examples:
42,x + y,3.14.
#include <fast_io.h>
int main()
{
using namespace ::fast_io::iomnp;
int x{5}; // x is an lvalue
int y{10}; // y is an lvalue
x = y; // OK: lvalue (x) = lvalue (y)
println("x = ", x);
x = 42; // OK: lvalue (x) = rvalue (42)
println("x = ", x);
// 42 = x; // ERROR: rvalue cannot be on the left-hand side
}
Key takeaway: The left-hand side of an assignment must be an lvalue. The right-hand side can be either an lvalue or an rvalue. This distinction is fundamental in C++ and becomes even more important when learning about references and move semantics.
Comparison Table
| Concept | When | Syntax | Example | Notes |
|---|---|---|---|---|
| Initialization | At object creation | =, (), {} |
int x{5}; |
Gives the object its first value. |
| Assignment | After object exists | = |
x = 10; |
Replaces the object’s current value. |
| Lvalue | Refers to memory location | Can appear on left-hand side | x = 42; |
Named objects, dereferenced pointers. |
| Rvalue | Temporary value | Can appear on right-hand side | x = y + 2; |
Literals, temporary results. |