• Forum
  • Lounge
  • I have discovered how to make global var

 
I have discovered how to make global variables work correctly

You need to do this to make it work:
9223372036854775804
9223372036854775805
9223372036854775806
#define global /##/
global int x;
global int y;
Last edited on
haw.

This is the trick I used to do way back when before I realized globals were horrible

1
2
3
//globals.cpp
#define GLOBAL
#include "globals.h" 

1
2
3
4
5
6
7
8
//globals.h

#ifndef GLOBAL
#define GLOBAL extern
#endif

GLOBAL int a;
GLOBAL int b;
I sometimes put "globals" in a key/string container with accessor functions. I'm not sure if that's better or not.

The only project I have that does it (or will, since I haven't written that part yet) is a shell I'm writing; it will use the container to store shell variables.
After primarily using Java for about a year seeing a global variable or function just looks weird (save for the main function)
Topic archived. No new replies allowed.