1 class TestClass1 2 { 3 public: 4 TestClass1() 5 { 6 cout << "TestClass1()" << endl; 7 } 8 TestClass1(const TestClass1&) 9 { 10 cout << "TestClass1(const TestClass1&)" << endl; 11 } 12 TestClass1(int i) 13 { 14 cout << "TestClass1(int i) " << i << " " <<endl; 15 } 16 }; 17 18 class TestClass2 19 { 20 public: 21 TestClass2() { cout << "TestClass2()" << endl; } 22 TestClass2(const TestClass2&){ cout << "TestClass2(const TestClass2&)" << endl; } 23 int fun() { return 2; } 24 }; 25 26 class TestClass3 27 { 28 public: 29 TestClass3(TestClass2& tc2):m_tc2(tc2),m_tc1(m_tc2.fun()) {}; 30 private: 31 TestClass1 m_tc1; 32 TestClass2 m_tc2; 33 }; 34 35 int main() 36 { 37 TestClass2 tc2; 38 cout << "................................................\n"; 39 TestClass3 tc3(tc2); 40 return 0; 41 }
结果:
明明TestClass1的初始化依赖TestClass2,为何TestClass1先于TestClass2初始化。
原文地址:https://www.cnblogs.com/XiaoXiaoShuai-/p/11625390.html
时间: 2024-11-09 03:40:02