making a globals/constants class

what is the standard method for making a static class? i want to make a class which stores all the globals/constants for my project so that i can include the class and use the constants throughout my project? also i am uncertain how constants function in c++ so i used regular variables. for my project i made a ProjectConstants class and defined all the variables in the constructor then made pointer variables for the object… but is there another method? thanks.

Hi ekene, you just need to put the keyword ‘static’ in front of any variables you want to access as a global.

// interface:
class AppGlobals {
static float myVar1;
static bool myVar2;
static int myVar3;

// implementation:
static float AppGlobals::myVar1 = 1.0f;
static bool AppGlobals::myVar2 = true;
static int AppGlobals::myVar3 = 2;

To access these variables, you need not create an instance of AppGlobals, but instead use it directly using the AppGlobals class name - so its very easy.

Many purists will tell you not to do this, that it goes against the idea of object oriented programming, you should pass an instance of your variables class to any object that may need them - or at least use a singleton etc… personally i think for small little experiments its perfectly fine :stuck_out_tongue:

im receiving and undefined symbol error. below is the structure of my class.

class globals{  
		static string gs1;    
		static string gs2;  

globals::globals() {  
		globals::gs1 = "gs1";    
		globals::gs2 = "gs2";