2013-05-03 121 views
0

我正在寫C++中的基本遊戲,其中包含3個類:Game類,Character類和Item類。在另一個類C++的構造函數中實例化一個類的對象?

Game類將擁有遊戲的所有邏輯,所以main函數只會簡單地創建Game object,稱其爲邏輯函數,遊戲將執行其他任何操作。可以有超過1名球員。

Character類有一個可以容納一個或多個Items的指針向量。一個角色可以有一個或多個項目

Item類具有該項目的所有屬性和功能。

我被困在設計遊戲的結構。有人建議我以創建Game object的方式構建我的遊戲,它也會創建一個Character object,然後該Character對象將創建一個用於保存Item的指針矢量,以及Item object。所以它喜歡當我打電話給constructor of the Game class時,它會調用constructor of the Character class,Character類的構造函數會自動調用constructor of the Item class

它是有道理的,但我無法弄清楚如何正確實施它。

這是我有 這是我到目前爲止有:

Game.cpp

Game::Game() 
{ 
     vector<Character*> characterVector; //to hold Characters 
} 

Game::startLogic() 
{ 
    string name; 
    Character character = new Character(name); 
} 

Character.cpp

Character::Character(string name) 
{ 
    m_name = name; 
    vector<Item*> itemVector; 
} 

Item.cpp

Item::Item() 
{ //initialise all the attributes of an item 
} 

main.cpp中

void main() 
{ 
    Game g; 
    g.startLogic(); 
} 

所以我可以創建一個角色,當遊戲運行(我仍然有該字符推入characterVector後來雖然),但我不是關於如何創建該項目十分肯定字符。我的意思是我應該在哪裏放置這個實例化代碼?在startLogic函數中,在Game的構造函數中,還是在Character的構造函數中?

+1

你卡在哪裏? – 2013-05-03 04:28:01

+0

也許你的大部分邏輯都會進入你的[god object](https://en.wikipedia.org/wiki/God_object)'Game'。 – Zeta 2013-05-03 05:03:19

+0

哦指針向量的原因基本上是cuz,這個遊戲實際上是關於與物品交互(組合,匹配)。角色就像玩家的化身。這只是爲了遊戲編碼的方式,它可以稍後擴展爲具有多個角色(玩家) – 2013-05-03 05:10:55

回答

1

Depositphotos你的載入中出錯地點。您需要將它們作爲類成員移入類聲明中,而不是作爲構造函數中的局部變量。這些構造函數可以用填充這個載體(但是真的,做角色知道它們是什麼樣的「天生」的東西,而且遊戲一開始就知道哪些角色是活着的?),但不應該聲明他們。

試試這個:

Game.h

#include <vector> 

class Character; 

class Game 
{ 
public: 
    std::vector<Character*> characters; 

    Game(); 
    ~Game(); 
    void startLogic(); 
}; 

Game.cpp

#include "Game.h" 
#include "Character.h" 
#include <memory> 

Game::Game() 
{ 
} 

Game::~Game() 
{ 
    for (std::vector<Character*>::iterator i = characters.begin(); 
     i != characters.end(); 
     ++i) 
    { 
     delete *i; 
    } 
} 

Game::startLogic() 
{ 
    ... 

    // using auto_ptr as a safety catch in case of memory errors... 

    std::auto_ptr<Character> c(new Character("Joe Smoe")); 

    std::auto_ptr<Item> i(new Item); 
    c->items.push_back(i.get()); 
    i.release(); 

    characters.push_back(c.get()); 
    c.release(); 

    ... 
} 

字符。ħ

#include <string> 
#include <vector> 

class Item; 

class Character 
{ 
public: 
    std::string name; 
    std::vector<Item*> items; 

    Character(std::string aName); 
    ~Character(); 
}; 

Character.cpp

#include "Character.h" 
#include "Item.h" 

Character::Character(std::string aName) 
    : name(aName) 
{ 
} 

Character::~Character() 
{ 
    for (std::vector<Item*>::iterator i = items.begin(); 
     i != items.end(); 
     ++i) 
    { 
     delete *i; 
    } 
} 

Item.h

class Item 
{ 
public: 
    Item(); 
}; 

Item.cpp

#include "Item.h" 

Item::Item() 
{ //initialise all the attributes of an item 
} 

的main.cpp

int main() 
{ 
    Game g; 
    g.startLogic(); 
    return 0; 
} 
+0

感謝您的真棒回答,我沒有期望收到像這樣的詳細答案。我想知道我是否必須將這些載體公諸於衆?遊戲的機制是當玩家開始時,他們將有機會用他自己選擇的名字創建角色,然後將角色的物品添加到角色的庫存 – 2013-05-03 08:37:35

+0

不,這些矢量不需要公開。只需將其他方法添加到「Character」類中即可根據需要添加/刪除/訪問項目。 – 2013-05-03 15:10:49

相關問題