ProgrammingVery hardProduct-company placement · C++

What is printed when compiled as C++17?

What is printed when compiled as C++17?
struct A {
    A()         { cout << "D"; }
    A(const A&) { cout << "C"; }
};
int main() { A a = A(A()); }
  1. A. DC
  2. B. D
  3. C. DCC
  4. D. Compile-time error: most vexing parse

Correct answer: B. D

Since C++17, initialising an object from a prvalue of the same type is guaranteed to construct it directly (mandatory copy elision); the nested temporaries are never materialised, so only the default constructor runs.

More OOP with C++ and Java questions

Exams that ask this topic