data structures - Instantiate an object of one class in the constructor of another class C++? -


i'm writting basic game in c++ features 3 classes: game class, character class , item class.

the game class have logic of game, main function create game object, call logic function , game else. there can more 1 player.

the character class has vector of pointers can hold 1 or more items. character can have 1 or more items

the item class has attributes , functionality of item.

i'm stuck @ designing structure of game. suggested me structure game in way when game object created, creates character object, , character object create vector of pointers hold item, , item object. likes when call constructor of game class, call constructor of character class, , constructor of character class automatically call constructor of item class.

it makes sense, couldn't figure out how implement it.

this i've got have far:

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 attributes of item  } 

main.cpp

void main() {     game g;     g.startlogic(); } 

so create character when game runs (i still have push character charactervector later though), i'm not quite sure on how create items character. means should put instantiate code? in startlogic function, in constructor of game, or in constructor of character?

your vectors in wrong places. need move them class declarations class members, not local variables inside constructors. constructors can fill vectors (but really, characters know items "born" with, , games know characters alive game start?), should not declare them.

try instead:

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() {     (std::vector<character*>::iterator = characters.begin();         != characters.end();         ++i)     {         delete *i;     } }  game::startlogic() {     ...      // using auto_ptr 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();      ... } 

character.h

#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() {     (std::vector<item*>::iterator = items.begin();         != items.end();         ++i)     {         delete *i;     } } 

item.h

class item { public:     item(); }; 

item.cpp

#include "item.h"  item::item() { //initialise attributes of item  } 

main.cpp

int main() {     game g;     g.startlogic();     return 0; } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -