0Pricing
C++ Academy · Lesson

When to Use Pointers vs References

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

When to Use Pointers vs References is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

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

Yes — the full text of “When to Use Pointers vs References” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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