so what I want is to have a global.h files that contain variables/functions for other classes/functions to use. For instance, say I have three header files: global.h, local1.h, and local2.h. Here, local1.h and local2.h are going to use variables from global.h. However, if I include global.h in both local1.h and local2.h, it would result in a multiple definition error as expected. I understand we can use the keyword "extern" but I heard its bad practice so I'm trying to avoid that.
I've tried to come along with this problem by using classes. Basically, I have a base class that contains variables that are all static. This way any class that would use those variables may just inherit from the base class. (in my case I'm fine with it as most of my programs are made out of classes). Below is the code,
Base.h
class base{
static const int SCREEN_SIZE = 1000;
};
class1.h
#include "base.h"
class class1: public base{
void printSize(){
cout << SCREEN_SIZE << endl;
}
};
class2.h
#include "base.h"
class class2: public base{
int getSize(){
return SCREEN_SIZE;
}
};
But I'm not sure if this is the best way to do it, any suggestions?
Here's my actual code:
#ifndef GAME_CORE_H
#define GAME_CORE_H
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
namespace Colors{
const SDL_Color RED = {255, 0, 0, 255};
const SDL_Color GREEN = {0, 255, 0, 255};
const SDL_Color BLUE = {0, 0, 255, 255};
const SDL_Color YELLOW = {255, 255, 0, 255};
const SDL_Color PURPLE = {128, 0, 128, 0};
const SDL_Color ORANGE = {255, 68, 0, 255};
const SDL_Color WHITE = {255, 255, 255, 255};
const SDL_Color BLACK = {0, 0, 0, 255};
};
namespace GAME_CORE{
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
SDL_Surface* gWindowSurface = NULL;
TTF_Font* defaultFont = NULL;
const int SCREEN_INIT_WIDTH = 1280;
const int SCREEN_INIT_HEIGHT = 720;
const int TILESIZE = 32;
void CORE_Init();
};
#endif // GAME_CORE_H
so the error msg showed multiple definitions of gWindow, gRenderer, gWidowSurface, and defaultFont