Accessing a global static variable from another class

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'm not sure how to do this correctly.

Just to clear my understanding of things, there is an internal connection in the global static variable, 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 to make a function that returns the address of a variable. Unfortunately this does not work.

// 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 can’t change the code in the header where the global static variable is declared. I can add functions, but I should try to avoid this if I can.

+7
source share
5 answers

You can select the main version of the container in one .cpp file and have a function that returns a link or a pointer to it. Then do not worry about other copies.

Wrapper.h

  anArray<someObject>& Objects(); 

Wrapper.cpp

 #include "someHeader.h" anArray<someObject>& Objects() { return SomeNameSpace::variable; } 

Or make the return value a const reference if you are not going to change the values.

+2
source

When you declare in your header file

 static int g_foo; 

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

When you announce

 class Foo { public: static int bar; }; 

then you get one static member, which should be defined in one .cpp file as int Foo::bar; . In this case, accessibility is defined as public.

+1
source

If the header file has a variable declared as static int a; , then yes, each translation unit including this header will receive its own copy of variable a and the problem will be guaranteed.

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

0
source

If it is declared as follows:

 MyClass myObj; 

then every .cpp file that somehow includes a 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 is declared as follows:

 extern MyClass myObj; 

then it is simply declared and it will work fine to include the header in several files, however it must be defined in the .cpp file.

0
source

Who creates an instance of the object? What happens if an object is accessible without instantiation?

Shouldn't it be put in a function that will create it?

...

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

EDIT: aClass.h

 static Object myObj; 

aClass.cpp

 aClass(const &Params params): myObj(params.x) { .... } 

bClass.cpp

 extern Object aClass::myObj; //not necessarily initialised bClass{ ... myObj.getSomething(); //problem maybes ... } 
0
source

All Articles