Variables, References, and auto
Declaring variables, C++'s reference type, and type inference with auto.
What you'll learn
- Declare a reference and explain that it's an alias for an existing variable, not a copy
- Use auto to let the compiler infer a variable's type from its initializer
- Predict that modifying a reference modifies the variable it refers to
Explanation
A C++ reference, declared with & after the type, creates an alias for an existing variable rather than a copy of it: int &ageRef = age; makes ageRef refer to the exact same memory as age. Unlike a pointer, a reference must be initialized at declaration (it can never be "null" or left unset), and it can never be reseated to refer to a different variable afterward -- once bound, ageRef always means age, for its entire lifetime. Reading or writing ageRef reads or writes age itself, directly, with no dereference operator needed.
C++11 introduced auto, which lets the compiler infer a variable's type from its initializer instead of you spelling it out: auto pi = 3.14159; deduces pi as a double, exactly as if you'd written double pi = 3.14159;. This is a genuine convenience for long or awkward-to-spell types (you'll see this matter more once templates and iterators are introduced later in this course), though for simple types like int or double, writing the type explicitly is often just as clear.
Together, references and auto are two of the ways modern C++ reduces boilerplate compared to plain C, while still being fully statically typed underneath -- auto doesn't make C++ dynamically typed; the type is still fixed once deduced, just inferred rather than written out by hand.
Guided lab
Predict: A reference and auto-deduced variable
Read this program and predict exactly what it prints.
#include <iostream>
int main() {
int age = 30;
int &ageRef = age;
ageRef = 31;
std::cout << "age: " << age << std::endl;
auto pi = 3.14159;
std::cout << "pi: " << pi << std::endl;
return 0;
}Stuck? Get a hint.
Common mistakes
- Trying to declare a reference without initializing it immediately -- unlike a pointer, a reference must be bound to a real variable at the moment it's declared.
- Assuming a reference can be reseated to refer to a different variable later, the way a pointer can be reassigned.
- Assuming `auto` makes a variable's type flexible or dynamic -- the type is still fixed at compile time, just inferred rather than written explicitly.
Knowledge check
Takeaway
A reference is a permanent alias for an existing variable (must be initialized immediately, never reseated); auto infers a fixed type from its initializer, reducing boilerplate without sacrificing static typing.
Summary
References alias an existing variable directly, with no dereference syntax needed; auto lets the compiler infer a variable's type from its initializer, still fully static underneath.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.