Take the 2-minute tour ×

I am working with code that has a global static variable (which is an object) and I need access to it from another class. I have always avoided global variables/functions in general so in this situation I am not sure how to go about it properly.

Just to clear my understand of things, in a global static variable has internal linkage, which means that any source file that includes this particular header will get its own copy of the variable?

EDIT: What I have tried so far is making a function which returns the address of the variable. Unfortunately, that does not seem to be working.

// names were changed but the code is as follows. 
// There is of course other code in the header
namespace SomeNameSpace
    static anArray<someObject> variable;

NOTE: I cannot change the code in the header where the global static variable is declared. I can add functions but I should try to avoid it if I can.

share|improve this question
Can you show how the global is declared? –  Timo Geusch Apr 4 '11 at 14:42
Is it a member of a class? Public member? –  Bo Persson Apr 4 '11 at 14:45
It is not a member of any class. @Timo: Please see the latest edit –  Samaursa Apr 4 '11 at 14:47
can you modify anArray? –  Nim Apr 4 '11 at 15:19

5 Answers 5

up vote 2 down vote accepted

You can decide on a master version of the container in one .cpp file, and have a function return a reference or pointer to that. Then don't bother with the other copies.


 anArray<someObject>& Objects();


#include "someHeader.h"

anArray<someObject>& Objects()
{ return SomeNameSpace::variable; }

Or make the return value a const reference, if you don't intend to change the values.

share|improve this answer

When you declare in you header file

static int g_foo;

and include this header file in multiple .cpp files, you get multiple instances one for each .cpp file that includes the header. These instances does not interfere at all. You can't communicate between the compilation units with these variables. Each instance is local to the compilation unit.

When you declare

class Foo
        static int bar;

then you get one static member that must be defined in one .cpp file as int Foo::bar; The accessibility is define as public in this case.

share|improve this answer
That I understand, but how do I go about accessing the former (that is, static int g_foo) from multiple .cpp files? –  Samaursa Apr 4 '11 at 14:51

If the header file has the variable declared as static int a; then yes, every translation unit that includes that header will get it's own copy of variable a and the trouble ensures.

If it is declared as extern int a, then the variable a is shared across all translation units where it is included, and is defined in some other file.

share|improve this answer

If it's declared like this:

MyClass myObj;

then each .cpp-file that in some way includes the header, possibly through other headers, will get a copy, and since they will all have the same name the linker will complain.

However, if it's declared like this:

extern MyClass myObj;

then it's just declared and it will work fine to include the header in multiple files, however it needs to be defined in a .cpp file.

share|improve this answer
It is declared like this: static anArray<someObject> variable; –  Samaursa Apr 4 '11 at 14:48

Who is instantiating this object? What happens if the object is accessed without it being instantiated?

Should you not put it in a function that will instantiate it?


anArray<someObject> aStaticVariable()
    static anArray<someObject> myStaticVariable;
    return myStaticVariable;

EDIT: aClass.h

static Object myObj;


aClass(const &Params params):


extern Object aClass::myObj; //not necessarily initialised
    myObj.getSomething(); //problem maybes
share|improve this answer
Well, since it is an automatic variable, I am assuming that it is going to be instantiated when the program starts. –  Samaursa Apr 4 '11 at 15:12
@Samaursa Yeah it will be instantiated when you attempt to use it, but not if it is external to the compilation unit. For example, if you use the extern static anArray<someObject> myStaticVariable;and you don't instantiate the containing class, then if you use it in a seperate class myStaticVariable.push(a);and it hasn't been initialised then you are in trouble. It might be initialised but the relative order of initialization of non-local static objects defined in different translation units is undefined. –  Dennis Apr 4 '11 at 15:32
The code does not have extern. Also, the relative order of initialization will only matter if I am trying to use the array at the program start time, no? If yes, then that is not an issue as I am accessing it at runtime while the program is running. If not (that is, it may not be initialized even at runtime), please correct my understanding of how static variables work in this case. –  Samaursa Apr 4 '11 at 15:39
Don't know how to put code in here properly... see original answer. –  Dennis Apr 4 '11 at 15:50

Your Answer


