Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Two simple questions 2

Status
Not open for further replies.

fugigoose

Programmer
Jun 20, 2001
241
US
Ok, my engine is coming along nicely. All the pieces are nearing completion and I'm approaching the part where they all need to talk to eachother. I have my engine set up in many different classes:

engine--
--renderer
--world
--console

Each class is declared in its own c++ file and they are all included in the main c++ file. Now I've run into the problem that world cannot call on console, etc. The only way I have been able to achieve this is to make a global function in the main file to parse the data like this:

void render(){
renderer.render()
}

This works, but it seems REALLY silly and wasteful to do this. Is there a way to make each global class instances see the other? What is the most effective way of doing this.

If it is not possible, then just answer me this: Can I create a pointer to class method or make a macro or something so one class can call it?

Thank you, and thanks everyone on tek-tips for your on-going support during my game devlopment.
 
I'm working on a game myself, and globals sometimes need to be used to save much time and effort. However, the situation you describe sounds like it might not need it. You could give the renderer, world, and console pointers to each other and access them this way. When you do things this way, your code often ends up not being very clean, but in my game programming experience, heavily optimized code is never very clean, anyway. To do things fast, sometimes you end up having to write ugly code. You might try the forums on gamedev.net for professional and other amature game programmers with more experience than me who can advise you better.

>> Can I create a pointer to class method or make a macro or something so one class can call it?

You can only make a pointer to a static method - it doesn't like the "this" pointer associated with non-static methods.
 
I thought about giving each class pointers to the other classes, however, when I try to declare it:

cls_renderer* pRenderer;

It doesn't know what the heck i'm talking about because that class is declared in a whole other file. Should I include the class definitions in each file, as opposed to just the main one, isn't that inefficient? I guess what I'm asking is how to make a pointer to a class instance that isn't declared in the same file as the pointer. Thanx.
 
Use class pre-declaration (for pointers only):
Code:
class FarClass;
// Now you may use FarClass* declarator before full dcl
Farclass* ptrOK;
...
Think about class static members: you can refere to it by class name qualification ClassName::MemberName. In case of soliton class (with only one object in the application) I have used pointer (or reference) to this object obtained via static member. This trick helps module isolation without any global variables.

Sometimes you may link your classes (more precisely, your class instances) via 'handshaking' (change of pointers). For example, your main function creates instances then calls its special members and pass proper pointers to them...

Avoid using of global variables. C++ presents more better tecnique(s) for safety objects cooperation...
 
>Should I include the class definitions in each file, as opposed to just the main one, isn't that inefficient?

Its inefficient if its included where its not needed, but the files that are operating on the class needs to see what it looks like. Include where needed (and only where needed).

Note that by using forward declaration in the header files (like ArkM showed), the headers usually dont need to include the class file, only the .cpp files that operates on the class needs to include it.

The headers need to include the class.h if
1) The class is a member variable
2) The class is inherited
3) Youre accessing stuff in the class' scope.
Ie a declaration like
Code:
SomeClass& foo(SomeClass*);
does not require the SomeClass.h to be included, but the implementation probably does.

The point is: If a.h includes b.h and b.h includes c.h, should you change something in c.h everything from a to c needs to be recompiled.

But if instead a.cpp includes b.h and b.cpp includes c.h.
Even though a has dependency to b that has a dependancy to c only b.cpp (and c.cpp naturally) needs to be recompiled if c.h changes. a is unaffected.

/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
Ok, I finally got around to trying the pre-definition thing. The problem is, when I reference the class it says that the class is not declared and that the item left of the -> is not of class/struct/union type. Here's what i'm trying to do:

Code:
//someClass.cpp
class someOtherClass;

class someClass{

public:
someOtherClass* pSomeOtherClass;
void someMethod(int someVar);

};

void someClass::someMethod(int someVar){
pSomeOtherClass->someOtherMethod(someVar); //bad
}

In my main c++ file i include the two different class definitions (each in its own c++ file). I then create instances of both and then give someClass a pointer to someOtherClass on start-up.

This however fails when it hits the line labeled 'bad'. I assume this is because at the time the compiler doesn't have a complete definition of the class, but if so, then what is the point of pre-defining a class? I need to be able to be able to declare each class independently, and have memebers in each that are pointers to classes that aren't necessarily fully defined. I need my someClass instance to be able to call on a method belonging to a someOtherClass instance.
 
>what is the point of pre-defining a class
If you declare someClass in the .cpp none - since you'll need access to it in the someClass methods' implementation anyway!

It makes sence in this situation:
someClass.h
Code:
class someOtherClass;
class someClass
{
public:
  someOtherClass* pSomeOtherClass;
  void someMethod(int someVar);
};

someClass.cpp
Code:
#include "someOtherClass.h"
...
void someClass::someMethod(int someVar)
{
  pSomeOtherClass->someOtherMethod(someVar); 
}



/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
You need to include the header of "someOtherClass" in the implementation file of "someClass" (in the cpp).
Although you declared the existence of "someOtherClass" (via forward declaration), the compiler needs to know its structure when you are using instances of that class for implementation purposes.

As I understand your problem, you want your classes to be loosely coupled. But you need the objects to work together, so they have somewhat to know each other. To work around this you may have a look on the Mediator design pattern. It helps to centralize the connections between classes in one class : the Mediator. I am using it for GUI purposes. Maybe you could have a look on this pattern, it is easy to find the general description on the InterNet.

--
Globos
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top