ProgrammingVery hardProduct-company placement · C++

What is printed?

What is printed?
struct B { B& operator=(int) { cout << "B"; return *this; } };
struct D : B { };
int main() { D d; d = 5; }
  1. A. B
  2. B. Nothing; 5 is converted to D
  3. C. Compile-time error: no operator= in D accepts an int
  4. D. Undefined behaviour

Correct answer: C. Compile-time error: no operator= in D accepts an int

D implicitly declares its own copy and move assignment operators, and they HIDE B::operator=(int). Neither accepts an int, so the call fails; using B::operator=; in D would make it print B.

More OOP with C++ and Java questions

Exams that ask this topic