C++ references: which statement about arrays of references and reseating is correct?
-
AAn array of references is acceptable.
-
BOnce a reference variable has been defined to refer to a particular variable it can refer to any other variable.
-
CAn array of references is not acceptable.
-
DReference is like a structure.
Answer
Correct Answer: An array of references is not acceptable.
Explanation
Introduction / Context:Unlike pointers, C++ references must bind to an object at initialization and cannot be left unbound. This property affects whether you can create arrays of references and whether a reference can later “point” somewhere else. The question probes both ideas through multiple choices.
Given Data / Assumptions:
- References must be initialized and cannot be reseated.
- Arrays require elements to be assignable entities with default construction rules unless explicitly initialized.
- We compare references with pointers and aggregates to clarify misconceptions.
Concept / Approach:
Because references must be bound at initialization and have no “null” state, standard C++ does not allow arrays of references as first-class types. You can, however, have arrays of pointers or arrays of reference wrappers (e.g., std::reference_wrapper) that emulate reference semantics. Additionally, a reference cannot later refer to a different variable, so claims of reseating are false. The notion that a reference is “like a structure” is baseless; it is an aliasing mechanism, not an aggregate type.
Step-by-Step Solution:
Assess array of references: disallowed by language rules ⇒ “not acceptable.” Assess reseating: impossible once a reference is bound. Assess “like a structure”: incorrect classification. Therefore, option “An array of references is not acceptable.” is correct.Verification / Alternative check:
Attempt to declare int& a[3]; Most compilers emit an error. Using std::reference_wrapper<int> a[3]; is legal, illustrating the preferred pattern when reference-like arrays are required.
Why Other Options Are Wrong:
Array acceptable — contradicts the language rule.
Reseating a reference — violates reference semantics.
Like a structure — a reference is not an aggregate type.
Common Pitfalls:
- Confusing arrays of references with arrays of pointers.
- Assuming a reference can be rebound after assignment; assignments through a reference modify the referent, not the binding.
Final Answer:
An array of references is not acceptable.