智能指针
来源:互联网 发布:hr管理软件知乎 编辑:程序博客网 时间:2024/05/17 05:18
#define _CRT_SECURE_NO_WARNINGS #include <iostream>#include <memory>using namespace std;class A{public:A(int a) {cout << "A(int)" << endl;this->a = a;}void func(){cout << "a = " << a << endl;}~A() {cout << "~A()" << endl;}private:int a;};class MyAutoPtr{public:MyAutoPtr(A * p){this->ptr = p;}~MyAutoPtr() {if (this->ptr != NULL) {delete this->ptr;}}//重载->操作符A* operator->(){return this->ptr;}//重载*操作符A& operator*(){return *ptr;}private:A * ptr;};//智能指针void test1(){#if 0A* p = new A(10);p->func();delete p;#endif#if 0auto_ptr<A> ptr(new A(10));ptr->func();#endifMyAutoPtr myPtr(new A(10)); //myPtr->func();(*myPtr).func();}int main(void){test1();return 0;}
阅读全文
0 0
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- CSS写三角
- 判断java中两个对象是否相等
- HTML 表单属性
- Java
- JSP 之 三大指令、四大基本语法、七大动作、九大内置对象
- 智能指针
- 每一步都是一大步
- 17. error: cannot connect to daemon(adb.exe start-server' failed启动失败,端口占用)
- Unity_API之monoBehaviour
- 回文子串
- Hello World
- 10010
- JavaScript 异步方案 async/await
- cocos2d创建项目使用bat批处理快速创建项目