ProgrammingModerateProduct-company MCQ pattern

Why must a C++ copy constructor take its parameter by reference (A(const A &other)) rather than by value?

  1. A. By value would make the object const
  2. B. References are faster but by value would also work
  3. C. Passing by value would itself call the copy constructor, recursing without end
  4. D. The compiler cannot copy private members by value

Correct answer: C. Passing by value would itself call the copy constructor, recursing without end

To pass an A by value the compiler must copy it, which means calling the copy constructor, which needs another copy, and so on forever. The language therefore rejects A(A other).

More OOP with C++ and Java questions

Exams that ask this topic