ProgrammingModerateProduct-company placement · GATE CS
Why does the C++ standard reject a copy constructor declared as T(T other) for a class T?
- A. Because constructors may not take parameters of their own class type at all
- B. Because passing the argument by value would itself need to copy a T, which would call this constructor again without end
- C. Because a by-value parameter cannot be const
- 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
- A drawing program uses the Visitor pattern: Shape has accept(Visitor v), and Circle implements it as v.visit(this). Visitor has overloads…
- How many of these statements about Java (9 or later) are correct? 1. An interface can declare instance fields. 2. An abstract class can…
- A UI component registers itself as an observer of a long-lived global EventBus when it is created but never unregisters. Screens using…
- A vending machine object behaves differently when Idle, HasCoin or Dispensing, and each state object decides which state comes next. A…
- Account.withdraw(amount) works for any amount up to the balance. A subclass FixedDeposit overrides it to throw an exception unless the…
- Which call violates the Law of Demeter (talk only to your immediate collaborators) inside an OrderService method that receives an Order…