when i was fooling around with xna i would create a general person class and npc, enemy's and even the character would derive from that class, i would then store it in one big array(list).
but it seems that in c++ it works a bit differently. could you suggest maybe a better way to accomplish this in a better way?
> is this good practice? doing it like this?
¿casting? no, I don't think so
> i would create a general person class and npc, enemy's and even the character would derive from that class,
> i would then store it in one big array(list).
if you pretend to threat them all as `general person' instead of going to the specific of each class, then it is fine.
But if you are going to test the type, then you are aware that that type exists, and you could have another collection for them.
> could you suggest maybe a better way to accomplish this in a better way?
I have no idea what you are trying to do
ok let me explain my current situation, for school i am required to make a game in the console window. i started off buy making menus.
i have a base menu class with some functions for changing the selected menu and some other stuff. now every menu draws its stuff differently so when i create a new menu class i derive from the base menu class that way i only need to change the draw function.
but now i have let say 7 menus and it's overhead in lines of code is pretty messy so i wanted to store the classes in a array so i could then iterate through to get the one i need, instead of 14 if statements. i c# it is no problem doing it the way i mentioned but as you sed in c++ its a different story.
so this is my question, how would you handle/ manage the classes to accomplish this.
class menu{
public:
virtualvoid draw();
};
class classic_menu: public menu{
public:
virtualvoid draw(){
std:;cout << "Classic\n";
}
};
//overload the virtual member function `draw()'
class keyboard_driven_menu : public menu{};
class simple_menu : public menu{};
class advanced_menu : public menu{};
std::vector< std::unique_ptr<menu> > Menus;
Menus.emplace_back( new classic_menu(/**/) );
Menus.emplace_back( new keyboard_driven_menu(/**/) );
Menus.emplace_back( new simple_menu(/**/) );
Menus.emplace_back( new advanced_menu(/**/) );
Menu[2]->draw();
If you only care about looks in the `draw()' function, another possibility would be
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class draw(){
public:
virtualvoid render();
};
class simple_draw: public draw{};
class menu{
std::unique_ptr<draw> d;
public:
void draw(){ //note: no virtual
d->render();
}
};
std::vector<menu> Menus(5); //note: holding objects
Menus[3].set_draw_method( new simple_draw() );
Menus[3].draw();