Dynamic memory allocation
new
new int;
new Stash;
new int[10];
new返回这个对象的指针
delete
delete p;
delete[]p;
1 #include <iostream> 2 using namespace std; 3 4 class A 5 { 6 private: 7 int i; 8 public: 9 A() 10 { 11 i = 0; 12 std::cout << "A::A()" << std::endl; 13 } 14 ~A() 15 { 16 std::cout << "~A::A(),i=" << i << std::endl; 17 } 18 void set(int i) 19 { 20 this->i = i; 21 } 22 void f() 23 { 24 std::cout << "hello" << std::endl; 25 } 26 }; 27 28 void main() 29 { 30 A *p = new A[10]; 31 32 for (int i = 0; i < 10; i++) 33 { 34 p[i].set(i); 35 } 36 37 delete[]p; 38 39 system("pause"); 40 }