C++ //继承中构造和析构顺序
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 class Base 6 { 7 public: 8 Base() 9 { 10 cout << "Base的构造函数!" << endl; 11 } 12 ~Base() 13 { 14 cout << "Base的析构函数!" << endl; 15 } 16 }; 17 18 class Son : public Base 19 { 20 public: 21 Son() 22 { 23 cout << "Son的构造函数!" << endl; 24 } 25 ~Son() 26 { 27 cout << "Son的析构函数!" << endl; 28 } 29 30 }; 31 32 void test01() 33 { 34 //Base b; 35 //继承中的构造和析构顺序如下: 36 //先构造父类 在构造子类 37 //析构相反 38 Son s; 39 } 40 int main() 41 { 42 43 test01(); 44 45 46 system("pause"); 47 48 return 0; 49 50 }
本文来自博客园,作者:Bytezero!,转载请注明原文链接:https://www.cnblogs.com/Bytezero/p/15114217.html