Archived

This topic is now archived and is closed to further replies.

SCRUB

global class throughout project

Recommended Posts

hello , im back with another problem. im trying to setup a class and an associated struct for global access throughout my project. I have a common.h included in all my headers to which in this I have the struct "con_var" ... my console variable structure. I need this global as they will be everywhere, and I also need have a ''project global'' class (conmanager) that manages these as well. There seems to be a bit of a loop over where I declare in common.h the con_var struct for conmanager.h and (vise versa) my ''extern conmanager'' for in common.h (for my proj global manager) Im confuesed as to where to declare my manager I think its suppose to me in a cpp and then to put an extern into the common.h so its global throughout the project ( I declared it in common.c ) can someone clarify my predicament pleaseeeeeeeeeeee thanks

Share this post


Link to post
Share on other sites
    
//game.cpp

#include "global.h"

cout << globalint;


//global.cpp

#include "global.h"

int globalint = 0;


//global.h

#ifndef GLOBAL_H
#define GLOBAL_H

extern globalint;
#endif


is that what you want?

Edited by - evilcrap on January 15, 2002 10:15:24 PM

Share this post


Link to post
Share on other sites
no , but thats how I understand to do.

but my predicament is that I have a class conmanager that has a list of cvars ... hmm in code this might looke a bit better.

  

/////////globals.h/////////////////////////

#include cvarmgr.h // proving a loop between global.h
// and cvarmgr

struct cvar_s
{
char *name;
char *string
float value;
}cvar_t; // global console variable declaration


extern cvarmgr_t *cvar_vars; // our "project global" console

varable manager

///////////globals.cpp ///////////////////

cvarmgr_t *cvar_vars // our declaration


//////////// cvarmgr.h///////////////////

#include "globals.h // loop


class cvarmgr_t
{
vector<cvar_s> list;
}


the problem is in the headers ... to have a global cvarmgr I need to include the header to get at the structure. Im gather this is a catch 22 situation and one of the things your not suppose to do in C++.

But what are the other options .... declare all project globals in Main and then pass them around and around having huge parameters ? *(not that i mind im just saying )

*hmmm maybye I should start looking at c++ game code rather than quake and quake 2 *

Share this post


Link to post
Share on other sites
To avoid that both files depend on each other make a forward declaration. Then you won''t have the need to include whatever.h in global.h.

// global.h
class xyz; // The forward declaration
extern xyz theGlobalThing;

By making a forward declaration you tell the compiler that it is ok to define variables of this type and that you''ll tell him later what exactly this class is. By doing so you have no need to include your classXYZ.h and the project should compile without (this ;-)) error.

Share this post


Link to post
Share on other sites