Global Objects

closed account (jw6XoG1T)
Im making a game engine right now and i need to make an object called "engine" from the Engine class available to all the other classes i made in other source files. How would i go about making a "global object"?

I know that variables have to be declared as:

header file 1: extern int a;
source file 1: int a = 1;

is there a similar method to make a class object available to everything within the program? Thanks
Class Objects are variables, I'm not sure what you're asking...?
closed account (jw6XoG1T)
i have a class called Engine....then i would create an object with it like so :

Engine engine;

How would i make "engine" global so all my other source files can access it and also be able to access it from within other different classes?
The exact same way you just described in your first post...
If you define an object in global scope then it is available to the whole application. You simply need to declare its existence before you use it by including its header file.

Header:
1
2
3
4
5
6
7
8
// Engine.h

class Engine
{
    // ...
};

extern Engine engine; // declare the existence of an Engine object called "engine". 

Implementation:
1
2
3
4
5
// Engine.cpp

#include "Engine.h"

Engine engine; // define the Engine object here 

Main:
1
2
3
4
5
6
7
8
9
10
#include "Engine.h" // provides declaration of Engine object

int main()
{
	engine.startup(); // use your Engine object
	
	// ...
	
	engine.closedown();
}


Hi, I believe if you want to make members of objects of type Engine to be shared by all objects of type Engine, you call it static. I could be wrong though. Good luck with your engine!
Topic archived. No new replies allowed.