博客
关于我
C++ 模版实现自定义释放类对象成员的堆内存空间
阅读量:583 次
发布时间:2019-03-11

本文共 941 字,大约阅读时间需要 3 分钟。

#include 
using namespace std;template
class AutoRelease{ typedef void (Type::* pFunc)(void);public: AutoRelease(Type* p, pFunc pfunc) :m_class(p), m_pFunc(pfunc) {} ~AutoRelease() { cout<< "release memory"<
*m_pFunc)(); }private: Type* m_class; pFunc m_pFunc;};class TestClass{public: TestClass(){cout<<"TestClass()"<
autoR(this, &TestClass::release); cout<<"excute()"<
testMem(); } void testMem(void) { if(NULL == ptr) { cout<< "ptr is NULL" <
tempVal<
excute(); p->testMem(); cout << (NULL==p)<< endl; p->excute(); p->testMem(); delete p; TestClass* p1 =new TestClass; p1->tempVal = 100; delete p1; delete p1; /* 把p1=NULL 的注释打开和关闭,运行对比下。 可以参考 深入探究C++对象模型 中“成员函数调用” 解释 */ //p1 = NULL; p1->testMem(); p1->testInt(); //思考:为什么delete之后还可以执行? NULL 还可以调用成员函数? p1->testInt(); p1->testInt(); return 0;}

转载地址:http://oiavz.baihongyu.com/

你可能感兴趣的文章
mongodb定时备份数据库
查看>>
mppt算法详解-ChatGPT4o作答
查看>>
mpvue的使用(一)必要的开发环境
查看>>
MQ 重复消费如何解决?
查看>>
mqtt broker服务端
查看>>
MQTT 保留消息
查看>>
MQTT 持久会话与 Clean Session 详解
查看>>
MQTT介绍及与其他协议的比较
查看>>
MQTT工作笔记0007---剩余长度
查看>>
MQTT工作笔记0008---服务质量
查看>>
MQTT工作笔记0009---订阅主题和订阅确认
查看>>
Mqtt搭建代理服务器进行通信-浅析
查看>>
MS COCO数据集介绍
查看>>
MS Edge浏览器“STATUS_INVALID_IMAGE_HASH“兼容性问题
查看>>
ms sql server 2008 sp2更新异常
查看>>
MS SQL查询库、表、列数据结构信息汇总
查看>>
MS UC 2013-0-Prepare Tool
查看>>
MSBuild 教程(2)
查看>>
msbuild发布web应用程序
查看>>
MSB与LSB
查看>>