c 模擬實現物件池

2021-09-28 16:30:18 字數 2486 閱讀 4716

怪物基類: 

#pragma once

//怪物列舉

enum monstertype;

class monster

virtual ~monster()//虛析構

monstertype gettype()const

private:

monstertype type;//怪物型別

};

三個子類怪物:

#pragma once

#include "monster.h"

#include class hmonster :

public monster

hmonster();

~hmonster();

};#pragma once

#include "monster.h"

#include class mmonster :

public monster

mmonster();

~mmonster();

};#pragma once

#include "monster.h"

#include class pmonster :

public monster

pmonster();

~pmonster();

};

怪物管理類:

#pragma once

#includeclass monster;

enum monstertype;

class monstermanager

~monstermanager();

public:

void addmonster(monster* monster);//1.新增到生成池中

void collection(monster* monster);//2.**到死亡池中

monster* findindeath(monstertype type);//3.在死亡池中查詢

private:

static monstermanager* pinatance;

monstermanager();

std::listlives;

std::listdeaths;

};#include "monstermanager.h"

#include "monster.h"

monstermanager* monstermanager::pinatance = nullptr;

monstermanager::monstermanager()

void monstermanager::addmonster(monster* monster)

void monstermanager::collection(monster* monster)

monster* monstermanager::findindeath(monstertype type)

} //將符合條件的從死亡池中移除

deaths.remove(monster);

return monster;

}monstermanager::~monstermanager()

for (monster* monster : deaths)

lives.clear();

deaths.clear();

//清空鍊錶

}

工廠類:

#pragma once

class monster;

enum monstertype;

//簡單工廠模式:建立物件

class monste***ctory

;#include "monste***ctory.h"

#include "monstermanager.h"

#include "monster.h"

#include "pmonster.h"

#include "hmonster.h"

#include "mmonster.h"

monste***ctory::monste***ctory()

monste***ctory::~monste***ctory()

monster* monste***ctory::create(monstertype type)

} pmanager->addmonster(monster);

return monster;

}

main函式:

#include "monster.h"

#include "monste***ctory.h"

#include using namespace std;

int main()

return 0;

}

QT 元物件模擬實現

1 qt元物件說明 qt 元物件q object 類似mfc的型別識別,不同之處在於,型別識別使用mfc的巨集定義,依賴語言,而qt是將元物件通過moc系統解析成中間檔案,模組之間通訊,可以包含相應的moc檔案,c 編譯moc,不同的moc儲存自己的qt物件資訊.2 qt訊號槽使用原型 class ...

C 模擬實現List

雙向鍊錶 include includeusing namespace std typedef nodenode templatestruct node t data 鍊錶中的資料 node pnext 下乙個節點 node ppre 前乙個節點 templateclass list templat...

c 模擬實現堆

這次我們來模擬實現堆,首先堆是乙個完全二叉樹,每個元素都有乙個關鍵碼,儲存相應的資料,堆分為最大堆和最小堆。最大堆 最大堆任意乙個節點都大於它左右孩子的關鍵碼,堆頂元素最大。最小堆 最小堆任意乙個節點都小於它左右孩子的關鍵碼,堆頂元素最小。因此我們得出以下結論 堆儲存在下標為0開始計數的陣列中,因此...