🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
>[info]# **顺序** 析构次序与构造次序严格相反.即栈式管理,**后创建的先被析构**. >[info]# **例子** ~~~ #include <iostream> using namespace std; ​ class Student { public:   Student()       //构造函数       {       cout <<"constructing student.\n";           semesHours=100;           gpa=3.5;       }       ~Student()       //析构函数       {       cout <<"destructing student.\n";       }   //其他公共成员   protected:   int semesHours;       float gpa; }; ​ class Teacher { public:   Teacher()     //构造函数       {       cout <<"constructing teacher.\n";       }       ~Teacher()   //析构函数       {       cout <<"destructing teacher.\n";       } }; ​ class TutorPair     //聚集类 { public:   TutorPair()   //构造函数       {       cout <<"constructing tutorpair.\n";           noMeetings=0;       }       ~TutorPair() //析构函数       {       cout <<"destructing tutorpair.\n";       }   protected:       Student student;     //对象成员       Teacher teacher;     //对象成员       int noMeetings; }; ​ int main() { TutorPair tp;   cout <<"back in main.\n";   return 0; } ~~~ 运行结果: ~~~ constructing student. constructing teacher. constructing tutorpair. back in main. destructing tutorpair. destructing teacher. destructing student. ​ //析构次序与构造次序严格相反.即栈式管理,后创建的先被析构. ~~~ # **冒号语法** * 又称为成员初始化列表.**其次序不影响成员的构造次序** * 构造或初始化时机:在冒号后,'{'之前.或认为就在'{'处. * 比在{}中定义的时机提前了