ProgrammingModerateProduct-company placement · GATE CS

Why does the C++ standard reject a copy constructor declared as T(T other) for a class T?

  1. A. Because constructors may not take parameters of their own class type at all
  2. B. Because passing the argument by value would itself need to copy a T, which would call this constructor again without end
  3. C. Because a by-value parameter cannot be const
  4. D. Because it would conflict with the implicitly declared move constructor

Correct answer: B. Because passing the argument by value would itself need to copy a T, which would call this constructor again without end

Initialising a by-value parameter of type T means copy-constructing a T, which is the function being defined, so the recursion never ends. The language therefore makes a constructor whose only parameter is T (by value) ill-formed; T(const T&) is the proper form.

More OOP with C++ and Java questions

Exams that ask this topic