C++ Academy · Lesson

When to Use Pointers vs References

Pick between pointers and references based on optionality and ownership semantics.

Lesson 4 of 413 steps

When to Use Pointers vs References is a free C++ Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

Different Tools for Different Jobs

References and pointers both let you indirect to another object — but with different semantics. Use each where it shines.

References for Mandatory Targets

A reference cannot be null and cannot be reseated. Use a reference parameter when the function always needs a real object.

void render(const Image& img);   // must have an image

Pointers for Optional Targets

A pointer can be null. Use a pointer when the argument is genuinely optional.

void render(const Image* img);   // nullptr means "skip"

Pointers When You Reassign

Pointers can be redirected to different objects. Use them when the indirection changes during the algorithm — like a moving cursor or current node.

Node* current = head;
while (current) {
    process(current);
    current = current->next;
}

References for Containers of References?

You cannot have a std::vector<T&>. Use std::vector<std::reference_wrapper<T>> or store pointers instead.

Returning References

Return a reference when the result outlives the call and the caller may use it directly (members, container elements).

std::string& Person::name() { return name_; }

Returning Pointers

Return a pointer when the function may return "no result" or when ownership is unclear. Prefer std::optional or smart pointers in modern code.

Class Members

Storing a reference as a member fixes the referent for the lifetime of the object — and forbids copy assignment. Pointer members are more flexible but require null checks.

Polymorphism

Both pointers and references can be used for polymorphism. Store base-class pointers in containers (vector cannot hold references); use references for parameters when null is invalid.

Smart Pointers Beat Raw Owners

Use raw pointers only for non-owning indirection. Ownership belongs to std::unique_ptr or std::shared_ptr.

Decision Cheat Sheet

Quick guide:

  • Always a valid object → reference
  • Might be absent → pointer or std::optional
  • Indirection that moves → pointer
  • Owns the memory → smart pointer
  • In a container → pointer (vectors of references are awkward)

Quick Check

Which is the best parameter type for a function that requires a non-null object?

Recap

Use references for mandatory, fixed targets; pointers for optional or moving targets. For ownership, prefer smart pointers. Containers usually need pointers, not references.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “When to Use Pointers vs References” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “When to Use Pointers vs References”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “When to Use Pointers vs References”?

Pick between pointers and references based on optionality and ownership semantics. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “When to Use Pointers vs References” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Raw Pointers and the Address-of Operator
  2. Dereferencing and Pointer Arithmetic
  3. nullptr and Null Pointer Safety
  4. When to Use Pointers vs References
← Back to C++ Academy