tags:

views:

211

answers:

8

I have a variable that I would like to use in all my classes without needing to pass it to the class constructor every time I want use it. How would I accomplish this in C++?

Thanks.

+1  A: 

Just declare it outside the class:

Header file:

extern int x;

class A {
  int z;
  public:
  A() : z(x++) {}
};

One source file:

int x = 0;
Alex Brown
where should i put it if i use a different header for each class and i wanna use it in all the classes ?
From.ME.to.YOU
you have to define it in a .cpp file. The "headers" (doesn't matter if they are in their respective .cpp's instead) must contain the `extern` keyword for that variable name
Stefano Borini
Most compilers support __declspec(selectany) int x = 0; in the header.
Hans Passant
A: 

Declare the variable as extern in a common header.

Define it in any source file.

Shmoopty
+2  A: 
global.h
extern int myVar;

global.cpp
#include "global.h"
int myVar = 0;  // initialize

class1.cpp
#include "global.h"
...

class2.cpp
#include "global.h"
...

class3.cpp
#include "global.h"
...

MyVar will be known and usable in every module as a global variable. You do not have to have global.cpp. You could initialize myVar in any of the class .cpp's but I think this is cleaner for larger programs.

fupsduck
A: 

keyword extern

//file1.cpp

int x = 0;

//file1 continues and ends.


//file2.cpp

extern int x; //this gets tied into file1.cpp's x at link time.

//file2.cpp goes on and ends
Paul Nathan
A: 

if you want to declare it in different header files/cpp files, just declare it extern outside of other files

//file1.c
int x=1;
int f(){}
//file2.c
extern int x;
Dasuraga
A: 
// L.hpp
struct L { static int a; };

// L.cpp
int L::a(0);
Justin
+2  A: 

While I would like to avoid global variables like the plague as our software cannot be multithreaded effectively due to the high reliance on global variables, I do have some suggestions:

Use a Singleton. It will allow you to keep the code and access clean. Part of the problem with a global variable is you don't know what code has modified it. You could set the value of global somewhere in your function relying on the hope that no one else will change it, but function your code calls, fooA, changes it and now your code is a) broken, and b) hard to debug.

If you have to use a global variable without touching the singleton pattern, look at fupsduck's response.

Lyndsey Ferguson
+1  A: 

If you're not going to use the Singleton pattern as Lyndsey suggests, then at least use a global function (inside a namespace) to access the variable. This will give you more flexibily in how you manage that global entity.

// mymodule.h
namespace mynamespace // prevents polluting the global namespace
{
   extern int getGlobalVariable();
}

// mymodule.cpp
namespace mynamespace
{
   int myGlobalVariable = 42;

   int getGlobalVariable()
   {
      return myGlobalVariable;
   }
}
Emile Cormier
I would suggest creating a setGlobalVariable(int newValue) function so that at least logging could be added to track the changes...
Lyndsey Ferguson