Hello!
I been trying to some kind structure my game data. In the book "Game Programming Gems" I find a Factory Pattern witch I pretty much understand but I can not get the syntax used in the example. The example looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
BasClass* ClassFactory::CreateObject(int id) {
BaseClass* pClass = 0;
switch (id) {
case 1:
pClass = new Class1;
break;
case 2:
pClass = new Class2;
break;
case 3:
pClass = new Class3;
break;
default:
assert("*Error! Invalide class ID passed to the factory!");
};
// pointed class common initialization
pClass->Init();
return pClass;
}
|
So
BasClass*
is a type of the
CreateObject
function. Cool idea, Then I could pass there my "game_state objects" and get initialized date in one place, also share the data between states truth the "BaseState"/"BaseClass". And there is a lot of other benefits of this structure. And I guess I could include all states header into this Factory so it will take care of all initializations.
The think I don't understand is how you make this
BaseClass*
.
I want it to represent States in my game so I write a simple parent class like:
1 2 3 4 5 6 7 8 9 10 11
|
class State {
public:
State(): id(0) {}
~State();
virtual void Init();
protected:
int id;
};
|
And then base for a state will look like:
1 2 3 4 5 6 7 8 9 10
|
class Menu: public State {
public:
Menu(): id(1) {}
~Menu() {printf("Menu state deleted");}
void Init() {printf("Menu state entered.");}
private:
};
|
But compiler does not see the "id" entry in children classes, even when I made it public, in the parent class,
../src/menu.h:13:10: error: class ‘Menu’ does not have any field named ‘id’
Am I thinking right about the "BaseClass"? Whats wrong with the code that code? Thank you!