Skip to content
Learn Netverks

Lesson

Step 8/36 22% through track

references-cpp

References

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches References: the syntax, patterns, and safety habits you need before advancing in C++.

Teams still ship References in C++ codebases—skipping it leaves gaps in debugging and code reviews.

You will apply References in contexts like: Game engines, trading systems, desktop apps, and performance-critical libraries.

Write C++ in main.cpp with int main(), click Run on server—the dev runner compiles with c++/g++ -std=c++17 -Wall and runs the binary; read template errors in stderr (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

A reference is an alias for an existing object: int& ref = x;. References must be initialized, cannot be rebound, and enable clean pass-by-reference semantics—unlike pointer reseating in C.

Pass by reference

void increment(int& n) { ++n; }

int x = 5;
increment(x);  // x becomes 6

const references

const int& gives read-only access without copying large objects—common in function parameters and range-for loops.

Important interview questions and answers

  1. Q: Reference vs pointer?
    A: References must bind to a valid object, cannot be null (unless referring to nullable wrapper patterns), and use . syntax; pointers can be null and reseated.
  2. Q: When use const T&?
    A: For input parameters to avoid copies while preventing modification.

Self-check

  1. Can a reference be left uninitialized?
  2. What happens to the caller's variable with pass-by-reference?

Tip: References cannot be null and must bind at initialization—safer than pointers for function parameters that always exist.

Interview prep

Reference vs pointer?

References must bind to a valid object, cannot be null, and cannot be reseated; pointers are more flexible but error-prone.

When use const T&?

For read-only input parameters—avoids copies while preventing modification.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Ref vs pointer?
  • const ref why?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump