State/class factory problem

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!
Last edited on
compiler does not see the "id" entry in children classes, even when I made it public

you are using member initialization in the Menu ctor, so the program expects Menu to have a data-member id but it is not declared anywhere in your code and the compiler cannot 'fetch' State's id data-member unless you tell it to. One way to do this would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

class State {
 public:
 State(int x): id(x) {}//overloading the State ctor 
  ~State();

  virtual void Init();

 public:
  int id;

};
class Menu: public State {
public:
  //  using State::id;
Menu(): State(1) {}
~Menu() {std::cout << "Menu state deleted \n";}

void Init() {std::cout << "Menu state entered \n";}

private:

};

Note you can't just declare anothe data-member id in Menu and expect the complier to know that this is the same as State's id data-member, instead it'll hide/shadow the base class data member and you'd have to use scope resolution (State::id) to make it explicit
how you make this BaseClass*.

just as your book did on line 2, by declaring the variable and initializing it though I'd prefer pClass = nullptr instead
btw printf() is C, not C++
So BasClass* is a type of the CreateObject function.

No. BasClass is a base class of Class1, Class2, Class3.

Line 14: assert() takes an int value, not a char array.
Topic archived. No new replies allowed.