7 试题
#include "stdafx.h"
#include "list"
using namespace std;
class TObject
{
public:
TObject(){m_id = 0;};
~TObject(){};
void setId(int id)
{
m_id = id;
}
int getId()
{
return m_id;
}
private:
int m_id;
};
class TObjectPool
{
public:
TObjectPool(unsigned int uLength)
{
for(int i=0;i
{
TObject* pObject = new TObject();
m_idllist.push_front(pObject);
}
}
virtual ~TObjectPool()
{
TObject *pObject = this->getObject();
while (NULL != pObject)
{
delete pObject;
pObject = this->getObject();
}
}
TObject* getObject()
{
list::iterator iter = m_idllist.begin();
if(m_idllist.end() != iter)
{
TObject *pObject = *iter;
m_idllist.erase(iter);
return pObject;
}
else
{
return NULL;
}
}
void releaseObject(TObject* pObject)
{
m_idllist.push_back(pObject);
}
private:
list m_idllist;
};
int main(int argc, char* argv[])
{
TObjectPool* pTPool = new TObjectPool(10);
TObject* pOject = pTPool->getObject();
pOject->setId(10);
pTPool->releaseObject(pOject);
while (true)
{
pOject = pTPool->getObject();
if(NULL == pOject)
{
break;
}
printf("%d",pOject->getId());
delete pOject;
}
return 0;
}
在windows执行上面程序, 下面正确的说法是(A)
A: 屏幕上输出 00000000010
B: 屏幕上输出 10000000010
C: 屏幕上输出异常
D: 以上说法都不正确
阅读(363) | 评论(0) | 转发(0) |