这么设计的模板有问题???!!!

门吹西风 2004-12-23 04:11:41

template <class T, class ARG_T>
class CQueue
{
public:
CQueue()
{
m_pHead = NULL;
m_pTail = NULL;
m_nCount = 0 ;
m_nIndex = 0 ;
m_bAutoDeleElement = FALSE ;
InitializeCriticalSection(&hCriticalSection);
}

~CQueue()
{
Clear();
DeleteCriticalSection(&hCriticalSection);
}
public:
int Append(ARG_T &element)//这里写引用还是 arg_t element ?
{
EnterCriticalSection(&hCriticalSection);
CElement *p = new CElement;
if ( p )
{
p->Data = element ;//参数是引用
if ( m_nCount )
{
p->Next = NULL;
p->Prev = m_pTail;
m_pTail->Next = p;
m_pTail = p;
}
else
{
m_pHead = p;
m_pTail = p;
}
}
LeaveCriticalSection(&hCriticalSection);
return ++m_nCount;
}

T &GetHead()
{
ASSERT( m_pHead != NULL);
EnterCriticalSection(&hCriticalSection);
CElement *p = m_pHead ;
T &t = p->Data;
if ( m_nCount )
{
m_pHead = m_pHead->Next ;
if ( m_pHead )
m_pHead->Prev = NULL ;
p->Next = NULL;
p->Prev = NULL;
delete p ;//这里删除P后, p->data也不存在了,我要返回其值,该怎么?
m_nCount -- ;
}
LeaveCriticalSection(&hCriticalSection);
return t;
}

void Clear()
{
EnterCriticalSection(&hCriticalSection);
CElement *p = m_pHead;
CElement *q = NULL;
while ( m_nCount && p)
{
q = p->Next ;
p->Data.~T();
delete p;
p = q ;
m_nCount -- ;
}
LeaveCriticalSection(&hCriticalSection);
}

int GetCount()
{
return m_nCount;
}

public:
struct CElement
{
CElement * Prev;
CElement * Next;
CElement()
{
Prev = NULL;
Next = NULL;
Data = NULL;
};
T Data;
};
private:
int m_nIndex;
int m_nCount;
BOOL m_bAutoDeleElement;

CElement *m_pHead;
CElement *m_pTail;

CRITICAL_SECTION hCriticalSection ;
};
...全文
32 点赞 收藏 2
写回复
2 条回复
anxiangbin 2005年01月11日
没有问题啊
回复 点赞
门吹西风 2005年01月11日
没有问题啊
回复 点赞
发动态
发帖子
VC/MFC
创建于2007-09-28

7898

社区成员

42.1w+

社区内容

VC/MFC相关问题讨论
社区公告
暂无公告