指针使用示例程序
程序示例
1 #include <iostream> 2 using namespace std; 3 class A 4 { 5 public: 6 A(); 7 ~A(); 8 int get() const{ return *i; } 9 void set(int x){ *i = x; } 10 private: 11 int *i; 12 }; 13 int main() 14 { 15 A *p = new A; 16 cout << p->get() << endl; 17 p->set(0); 18 cout << p->get() << endl; 19 delete p; 20 return 0; 21 } 22 A::A() 23 { 24 cout << "构造函数执行中~~~~\n"; 25 i = new int(999); 26 } 27 A::~A() 28 { 29 cout << "析构函数执行中~~~\n"; 30 delete i; 31 }
结果: