Global Class Objects?

Started by
4 comments, last by anttoo 12 years, 9 months ago
Im making a game engine and i need to be able to make my engine object available to all the other classes in the other source files.

I have the engine declared as so:

Engine engine;

How would i make "engine" global so that i could use it within other classes in other source files?
Advertisement
You might want to read this article.
Thats not really telling me how to fix my problem tho :P i need to make it global

Thats not really telling me how to fix my problem tho :P i need to make it global


Have you really read the article? It does cover your problem.

Thats not really telling me how to fix my problem tho :P i need to make it global


No you don't.


If an object requires to know the engine, then simply pass the engine to the object (or the appropriate subsystem). For example, imagine a class that represents a mesh. The mesh requires access to the rendering system when it is to be rendered, but instead of using one global engine, simply pass the engine either at creation time or during rendering to the mesh.

I wouldn't mind you using globals however this kind of attitude has corrupted our codebase at work, so I took it upon myself to haunt anyone promoting the use of globals (there are VERY view cases in which globals are THE way to go).


Im making a game engine and i need to be able to make my engine object available to all the other classes in the other source files.

I have the engine declared as so:

Engine engine;

How would i make "engine" global so that i could use it within other classes in other source files?



I have a class like this:

//GameCore.h
class GameCore
{
public:
void someNormalFunction()
void daDaDa()
.....
static GameCore* Get(); //This returns a global object
}

//GameCore.cpp

GameCore *nastyGlobal;

GameCore::Get()
{
if(nastyGlobal == NULL)
nastyGlobal = new GameCore()

return nastyGlobal;
}

//otherFile.cpp

....
GameCore::Get()->PlayGame();
GameCore::Get()->Stop();

This topic is closed to new replies.

Advertisement