Skip to main content
GameDev.net gamedev.net
🔒 Locked

Class Question

Started by Zimans Jan 13, 2003 at 8:09 PM 3 replies 200+ views
Original Post
Zimans
Zimans
This may be a trivial thing but i''m not sure how this would work. I have two classes. Worldmap and EntManage. Both classes need to be able to access public functions/members in the other class. How do I go about making it such that they can talk? I''m not sure if this is possible as I was thinking about it. If you define WorldMap, then EntManage hasn''t been defined yet, so you can''t define WorldMap. The same goes with the include files. CEntManage.h can''t be included cause it wants to include CWorldMap.h, However CWorldMap.h wanted to include CEntMagage.h in the first lace. The two solutions I''ve thought of are to either make all communication one way, say EntMagage asks WorldMap for various things. Or I merge EntManage int WorldMap. I suppose another option is to move the section of code int WorldMap that needs to talk to EntManage to another class, or a single func, then make that class/func a friend class of WorldMap so it can still access private members to accomplish its task. The idea is that WorldMap handles the map itself. It loads it, provides collision/entvis/etc. routines, and renders the map based off a position and view vector. EntManage handles Entities. It caches Ents, manages the game Ent list, and loops through Ents allowing them to process and render. (The Ents themselves do there own rendering) The Ents will also be passed to the VM class so that they can think. This brings up another case. The VM needs access EntManage so that it can test an Ent against the other Ents for collision. But EntManage needs access to the VM so it can incurr an Ent to think. Maybe I just really need to rethink my class structure.. Any Suggestions? -Zims
Zimans
Zimans
Well, I guess I just answered my own question, atleast the first part of the question. Just need to give the header a class prototype, then include the actual header in the .cpp file.

What''s an elegant way to do this? do I include the actual header in the .cpp, and just give the current class header a prototype?

Is there a way to make it such that I can include the header in both the .cpp and the .h and have it give the .h just a prototype and the .cpp the actual defenition?

WHat is a good way to pass headers around and not run into infinte recursive loop includes..

-Zims
Learning new things everyday.
Zipster
Zipster
I''m still a little unclear as to how your stuff is setup, but forward declaration might help you out. Instead of including the header file for a class, simple forward declare it near the top of the file, before it is used:

class MyClass;

This tells the compiler, "Hey, I have this class called MyClass, and it exists, so no bitching!". The only drawback is that since it doesn''t actually have any information on the class, you can only use it as a pointer, not an actual instance. By the same token you can''t call functions from it until the compiler knows the actual definition of the entire class. Basically, anything that would require inside knowledge of the class, you can''t do with only a forward declaration.
Zimans
Zimans
Thats basically what I figured out. What I do is this: In my header I use some pre-processor commands to either give the full class, or just the prototype if the class was already in the process of being defined.

#ifndef HEADERNAME_H
#define HEADERNAME_H
class MyClass
{
yadda yadda
};
#else
class MyClass;
#endif

this way, when ClassA includes ClassB.h, wich includes ClassA.h Classb.h gets a class MyClass; only cause ClassA was being defined. This makes everything happy.

-Zims
Dave Hunt
Dave Hunt
Simpler would be:

ClassA.h -

#ifndef _CLASSA_H
class ClassB;
class ClassA
{
yada yada
};
#endif

ClassB.h -

#ifndef _CLASSB_H
class ClassA;
class ClassB
{
yada yada
};
#endif

Then, in both ClassA.cpp and ClassB.cpp -

#include "ClassA.h"
#include "ClassB.h"

Having said all that, if you have two classes that need heavy access to each other''s members/methods, then you may need to rethink your design.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.