看程序写结果,含有内嵌对象的类的构造函数调用次序
#include <iostream> using namespace std; class Student1{ public: Student1() {cout << "Student1+" << endl; } ~Student1() { cout << "-Student1" << endl; } }; class Student2{ public: Student2() {cout << "Student2+" << endl; } ~Student2() { cout << "-Student2" << endl; } }; class Teacher{ public: Teacher() {cout << "Teacher+" << endl;} ~Teacher() { cout << "-Teacher" << endl; } private: Student2 stu2; Student1 stu1; }; int main(){ Teacher t; return 0; }
结果表明,先按照类成员的声明次序调用相应类的构造函数
Student2+ Student1+ Teacher+ -Teacher -Student1 -Student2
标签:内嵌,真题,19,Student1,次序,函数调用 From: https://www.cnblogs.com/uacs2024/p/18078956