C++ references: evaluate two claims A reference is not a constant pointer. A reference is automatically dereferenced when used. Choose the single best option.

Difficulty: Easy

Correct Answer: Both 1 and 2 are correct.

Explanation:


Introduction / Context:
This item distinguishes C++ references from pointers and highlights how expression semantics differ. References behave like aliases to objects, removing the need for explicit * dereferencing that pointers require. The question asks you to validate two common statements about references.


Given Data / Assumptions:

  • Statement 1: A reference is not a constant pointer.
  • Statement 2: A reference is automatically dereferenced in expressions.
  • Standard C++ (no vendor-specific extensions) is assumed.


Concept / Approach:

References are not pointer variables; they do not store an address that you can reseat. Instead, the reference name is another name for the same object. When you write operations using a reference, the language automatically accesses the underlying object; hence there is no need to write *r to get the referent. This automatic dereferencing is why code using references often looks cleaner than the equivalent pointer code. Therefore, both statements align with core C++ rules.


Step-by-Step Solution:

Evaluate statement 1: true — a reference is an alias, not a constant pointer variable. Evaluate statement 2: true — expressions on a reference are applied to the referent directly, with no explicit * required. Conclude both statements are correct.


Verification / Alternative check:

Example: int x = 10; int& r = x; r++; After increment, x == 11. No *r was needed. Attempting to reseat r to another variable fails, confirming it is not a pointer that can be reassigned.


Why Other Options Are Wrong:

Only 1 or only 2 — each ignores the correctness of the other statement.

Both incorrect — contradicts well-defined reference semantics.


Common Pitfalls:

  • Misinterpreting “automatic dereference” as “magically safe lifetime”; references still require valid lifetimes of referents.
  • Confusing references with pointers regarding nullability and reseating.


Final Answer:

Both 1 and 2 are correct.

More Questions from References

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion