Back to questions
JuniorTheoryCommon
What does the program print?
#include <iostream>
struct Note {
Note(char c) { std::cout << c; }
};
struct Pair {
Note first;
Note second;
Pair() : second('2'), first('1') { std::cout << '3'; }
};
int main() { Pair p; }123
- Expecting
213: fields are initialized in the order they are declared in the class, not in the order the initializer list names them. - Expecting
312: every field is initialized before the constructor body starts, so the body prints3last.
- What goes wrong if one field's initializer reads a field declared below it?
- In what order are
firstandseconddestroyed whenpgoes out of scope?