ProgrammingHardMicrosoft/Amazon SDE MCQ · GATE CS

What is printed when compiled as C++17?

What is printed when compiled as C++17?
struct T {
    T()           { cout << "c"; }
    T(const T&)   { cout << "k"; }
};
T make() { return T(); }
int main() {
    T a = make();
    T b = a;
}
  1. A. ckkk
  2. B. ckk
  3. C. cc
  4. D. ck

Correct answer: D. ck

Since C++17 a prvalue initialises its target directly, so T() inside make() constructs a itself: one 'c' with no copy. T b = a copies an lvalue: one 'k'.

More OOP with C++ and Java questions

Exams that ask this topic