使用模版类创建对像 防止忘记释放new的对像

因为VC6.0不能使用智能指针,所以写出此类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 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_)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 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
//