一个常常用到的管理类

来源:互联网 发布:如何在淘宝申请品牌 编辑:程序博客网 时间:2024/05/22 02:05

类似这种管理类,经常的写。烦死了,留一个在这里,以后就直接拷贝了

如果有错误,请指出,谢谢

 

头文件

#include <Windows.h>

#include <list>
using namespace std;

struct Player
{
 DWORD   PlayerID;
 string  strName;
 int  nLevel;
 int  nScore;
};

class CScoreManage
{
public:
 CScoreManage(void);
 ~CScoreManage(void);

 CScoreManage(int nCount);

 bool InitScore(int nCount);

 typedef list<Player*> ListPlayer;

 ListPlayer m_ListPlayer;

 bool UpdateInfo(Player& player);

 void DeletePlayer();
};

extern CScoreManage ScoreManage;

///////////////////////////////////////////////////////////////////////////////////

cpp文件:

 

#include "ScoreMag.h"

CScoreManage::CScoreManage(void)
{
}

CScoreManage::CScoreManage( int nCount )
{
 InitScore( nCount );

}

CScoreManage::~CScoreManage(void)
{

 DeletePlayer();
}

bool CScoreManage::InitScore( int nCount )
{
 bool bRs = false;
 for (int i= 0;i < nCount; i++)
 {
  Player* pPlayer = new Player;
  if (!pPlayer)
  {
   return bRs;
   
  }
  pPlayer->nLevel = 0;
  pPlayer->nScore = 0;
  pPlayer->PlayerID = 0;
  m_ListPlayer.push_back(pPlayer);
  
 }
 bRs = true;
 return bRs;

}

bool CScoreManage::UpdateInfo( Player& player )
{

 bool bRs = false;
 ListPlayer::iterator ite;
 for (ite = m_ListPlayer.begin(); ite != m_ListPlayer.end(); ite++)
 {
  if ( (*ite)->PlayerID == player.PlayerID)
  {
   (*ite)->nScore = player.nScore;
   (*ite)->nLevel = player.nLevel;
  }
 }
 return bRs = true;

}

void CScoreManage::DeletePlayer()
{
 ListPlayer::iterator ite;
 for (ite = m_ListPlayer.begin(); ite != m_ListPlayer.end(); ite++)
 {
  if (*ite)
  {
   delete *ite;
   *ite = 0;
  }
 }
 m_ListPlayer.clear();

}