Chinaunix首页 | 论坛 | 博客
  • 博客访问: 573628
  • 博文数量: 493
  • 博客积分: 2891
  • 博客等级: 少校
  • 技术积分: 4960
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-17 17:11
文章分类

全部博文(493)

文章存档

2010年(493)

分类:

2010-05-12 19:36:31

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) |
给主人留下些什么吧!~~