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
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!