ODBC连接数据库,防止忘记释放内存之使用模版类 | 火苗999℃的博客
使用模版类创建对像 防止忘记释放new的对像
代码
因为VC6.0不能使用智能指针,所以写出此类
// RecordsetObject.h: interface for the CRecordsetObject class.
//
//
#if !defined(AFX_RECORDSETOBJECT_H__3DCAE528_1138_4312_BCD9_052B5750324A__INCLUDED_)
#define AFX_RECORDSETOBJECT_H__3DCAE528_1138_4312_BCD9_052B5750324A__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/
// creat BYZT 13.07.08
/
template<class T>
class CRecordsetObject
{
public:
inline BOOL operator==(const T *p2)
{
return __pTaskGunInfo == p2;
}
inline T*& operator->()
{
return __pTaskGunInfo;
}
public:
CRecordsetObject(CDatabase* pDatabase);
virtual ~CRecordsetObject();
private:
// CTaskGunInfo对像指针
T *__pTaskGunInfo;
// 禁止使用的方法
private:
CRecordsetObject(const CRecordsetObject&);
CRecordsetObject operator =(const CRecordsetObject&);
};
template<class T>
CRecordsetObject<T>::CRecordsetObject(CDatabase* pDatabase)
:__pTaskGunInfo(NULL)
{
__pTaskGunInfo = new T(pDatabase);
}
template<class T>
CRecordsetObject<T>::~CRecordsetObject()
{
if (NULL != __pTaskGunInfo)
{
if (__pTaskGunInfo->IsOpen())
{
__pTaskGunInfo->Close();
}
delete __pTaskGunInfo;
__pTaskGunInfo = NULL;
}
}
#endif // !defined(AFX_RECORDSETOBJECT_H__3DCAE528_1138_4312_BCD9_052B5750324A__INCLUDED_)
// RecordsetObject.cpp: implementation of the CRecordsetObject class.
//
//
#include "stdafx.h"
#include "RecordsetObject.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// Construction/Destruction
//
更新
转载请注明来源:http://blog.csdn.net/defaultbyzt