# Game Libary Structure Problem

This topic is 4844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Okay so Im making my engine and I have one problem, it's like this(MUCH Simpler version):
class Ram
{
public:
RDraw					Draw;
RMatrix					Matrix;
};


But these classes arent finished yet, see, Matrix needs to use the Draw Object, (to set the matries to the stack), and Shader needs to use both Matrix and Draw. So Im sure many of you have smoked this problem easy. Ive tried making Draw all static vars and static functions but then when I use it I get LNK2001 Error. If this is not normal, please say so and ill provide more code for the problem.

##### Share on other sites
Actually, your problem is that you try to do some mutually recursive structures. For example:

class A
{
public:
B b;
};

class B
{
public:
A a;
};

is not implementable. What you can do is make a forward declaration, and use pointers:

class B; // Forward declaration: we will use this object

class A
{
public:
B * b; // The size of class A is 4, because b is a pointer. No compilation problem!
};

class B
{
public:
A a; // A is known, no need to use a pointer
};

You can also use references, which are generally safer, but in this case you will need to set them at the object construction, and that is not always possible.

##### Share on other sites
Ah I see, so I dont have to change the structure of my program much at all. Thank you

##### Share on other sites
Actually, this cross-access of classes is a royal p.i.t.a. for most people who are designing their own engine. Often it comes down to access levels and variable scope in the end.

Often you have your 'GameEngine' class, which contains a 'Renderer', 'Sound', 'ObjectManager' and 'UserInput' object. The code inside some of the objects will need to call functions of one of the other objects, but can't 'see' it because they are instanced by a parent object.

There are a few ways around this:

* Lots of object references and pointers flying around all over the place.
* Using Singletons
* Putting any cross-object code up a level into the GameEngine class.

Just my 2 cents

Synex
Code Monkey

• 17
• 10
• 19
• 14
• 19