class inheritance

Hello,

My problem is I think mainly a design one.
I have a node structure :

1
2
3
4
struct Node : public HashTable::Node
{
/** Random variables and functions **/
};


then I create a class like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class StateStorage
{
   protected:
      HashTable table;

      struct StateNode : public Node
      {
      /** basic variables **/
      };
      std::allocator<StateNode> nodeAllocator;

   public:
      /** random functions **/

}


From this I would like to create other classes while adding in them members to the StateNode structure, for example :

1
2
3
4
5
6
7
8
9
10
11
12
13
template <class T> class ActionStateStorage : public StateStorage
{
   protected :
      struct StateNode : public StateStorage::StateNode
      {
         /** adding some stuff here **/
         T actions[];
      }

   public :
      /** random functions **/

}


Now I have multiple different classes like ActionStateStorage that derive from StateStorage.

But the problem is all the functions that handle the nodes via the nodeAllocator are written in the StateStorage to simplify and make the code a LOT smaller.

As you see StateStorage::StateNode != ActionStateStorage::StateNode, so it creates a lot of problems and I really don't know how to solve them.

So what I want is, when I instantiate a ActionStateStorage and creating a new StateNode via a function defined in the StateStorage class I'm creating a ActionStateStorage::StateNode and not a StateStorage::StateNode.

I hope it isn't too much confusing.

Sorry for my bad english if I made mistakes.

Thanks.

The only solution I came up with is to make the Node a class then defining StateStorage in a different way, I know in java it would be written approximately like this :

template <T extends Node> StateStorage....

But I don't even know how to write it in C++ ><
Last edited on
If you are inheriting from StateStorage to create ActionStateStorage then ActionStateStorage will have the definition for StateNode already. There is no need to re-declare it.

That does look an awful lot like Java code? Am I Correct?

Have a read up on how to use the C++ map container. This offers the same functionality (basically) as the hash table. But it's a bit simpler in it's implementation.

http://www.sgi.com/tech/stl/hash_map.html

Z.
Topic archived. No new replies allowed.