1 #include <iostream> 2 using namespace std; 3 4 class as 5 { 6 public: 7 int a; 8 const int b; 9 as():b(2) 10 { 11 a = 1; 12 } 13 ~as() 14 { 15 cout << "析构函数被调用!" << endl; 16 }; 17 18 19 private: 20 int c; 21 22 }; 23 24 int main(int argc, char *argv[]) 25 { 26 as b; 27 as c; 28 cout << b.a << endl; 29 cout << b.b << endl; 30 31 return 0; 32 }
1 #include <iostream> 2 using namespace std; 3 4 class A 5 { 6 public: 7 A() 8 { 9 cout << "A()" << endl; 10 } 11 12 ~A() 13 { 14 cout << "~A()" << endl; 15 } 16 }; 17 18 int main(int argc, char *argv[]) 19 { 20 A a; 21 A* p = new A[3]; //不会自动释放 22 return 0; 23 }