I'm writing a game framework and I'm trying to generalize and encapsulate platform dependent code like the renderer so that it makes it a bit easier to port. I'm trying to accomplish this while still having a clean way of using the framework. I'm currently having a problem with static variables and namespaces...
// Renderer.h
namespace Renderer
{
static IRenderer* g_pRenderer = NULL;
static IRenderer* Get(void) { return g_pRenderer; }
static IRenderer* CreateD3DRenderer()
{
g_pRenderer = new RendererD3D(); // Derived from IRenderer
return g_pRenderer;
}
}
So in my main(), I can call CreateD3DRenderer() and it returns an instance just fine; g_pRenderer is holding its value since it's being created and returned within the scope of its function... However, Renderer::Get() returns NULL. Removing 'static' from the init of g_pRenderer causes clashes when used in other files.
What's going on?
First off, void in an argument list is only necessary in C. In C++, you'd just write Get().
As for the main question, static variables are restricted to the compilation unit. Since you put them in a header, this results in a separate variable being created for every compilation unit (i.e. every cpp file) where it is included. You get errors when you remove the static part because then you have multiple variables with the same name, leading to a linking error.
If you want a single variable to be shared between multiple files, you use extern. However, this is considered bad practice. You're better off refactoring so you don't need global variables like that.
You should keep all definitions of functions in .cpp files, instead of .h
What is happening is that when static is used in that fashion, the function or variable is restricted to that .cpp file, which means that g_pRenderer is different in each .cpp file. Multiple definition doesnt happen since each definition is restricted to one .cpp file.
You need to remove it to get your global pointer to work. You can keep the static on the getter function, but I would remove it. You should move the definition of the function and the global variable to a .cpp file and keep just a declaration of each in the .h. Declare the variable as extern.
Related
Many other questions deal with how to allocate a variable by declaring it in a header file and defining it (allocating) in a .cpp file.
What I want to do is not use any .cpp files for my class, and to define all functions as inline (in the header file). The problem that I run into is how to define static member variables so that even when the .h file is included in multiple compilation units I don't get the "first defined here" linker error.
I'm open to preprocessor hacks, etc. if it gets the job done. I just want to avoid any .cpp files.
If it matters I'm using GCC.
You can abuse the singleton pattern if you really must avoid any .cpp files:
class Foo {
public:
static Bar& getMyStatic() {
static Bar bar;
return bar;
};
};
This works because now the variable is a static variable inside a function, and static has a different meaning within a function context than within a class context. And for functions, the linker does recognize multiple identical definitions and throws away the copies.
But, of course, I would strongly advise against avoiding .cpp files: It means that you get into a situation where you have to build the entire program, or at least large parts of it, in one big piece. Every change you do will necessitate a complete rebuilt which slows down your change-compile-test cycle significantly. For very small projects that might not be a problem, but it is for medium to large ones.
With static variables you have to put in a .cpp file to avoid the possibility of multiple static variables when the intention is to have just the one. Besides it is not a good idea to have large inline methods as it is only a hint to the compiler but also makes compilation take longer (you change some of those functions in development and then lots of dependent files will need to get compiled!)
However if you do not want lots of .cpp files with just a few statics in it why not have just one file to store them in.
As long as you only include that header file once in your whole project, you'll be OK. However, that's a pretty strong requirement, and can be difficult to make others adhere to.
You could have a static variable, but that means you have more than one for the entire program, which may or may not matter (bear in mind that you can't change it in the future, so you may have what's known as a "latent bug" - you change some other code, and all of a sudden you have created a new bug, because the variable isn't ONE variable).
I am trying to create bunch of void methods and call them later in program.
I will demonstrate the code ahead to help better understand my issue.
.h file
static float sfloat;
namespace someNamespace
{
static void foo();
}
.cpp file
void someNamespace::foo(){cout<<sfloat<<endl}
above code is simpler version of class that I was working on.
I initialize sfloat in other .cpp file
otherFile.cpp
void initializeAndUseFoo(){sfloat = 5; someNamespace::foo();}
As far as my understanding goes, I expect foo to print out 5 but it prints out 0 instead.
This behavior occurs across all other static variables that I have as well(pointer included). It seemed that somehow the variables inside the function are never initialized as the value I assign.
However if I call out "sfloat" not through the function then I can call it out properly.(if I just print it out on console with just
cout<<"just print it not through the function : " <<sfloat<<endl;
then it is indeed 5
Thank you for reading.
A global static variable is static within one compilation unit. If you make another compilation unit, it will have a separate copy of that static variable.
This SO question explains what a compilation unit is in C++.
Since your variable is static and global in the same time, there will be a single instance of it in it's compilation unit. If you want to access that exact variable from another file, you will have to use the extern keyword. Without it, a new copy of that variable will be created in each compilation unit. You can find some information here, for example.
That's not a class, it's a namespace. static inside the namespace or at global scope gives methods and variables internal linkage. That means a copy of each will be available for each translation unit.
Because you modify (not initialize) sfloat in otherFile.cpp, only that version of the variable is modified. The original, initialized in .cpp file, retains the same value (which is the version printed by someNamespace::foo().
I have a C++ DLL with 3 classes in it. I added a static boolean variable to my "stdafx" header file (since all of my classes include it) and am trying to use it. While all of my classes see my variable, they each seem to have a different instance of it. If I set the variable to true in once class then I'll notice that it's false in another class. Is there any way that I can create a variable that can be used across all classes only within the DLL?
Well, you labelled it static, so that's what happens. Instead, label it extern in headers and define it in one TU.
And don't modify stdafx; it's not yours. Use your own shared headers.
Your variable is static and you're declaring it in stdafx.h which is included by all source files in your project. This means each translation unit will contain its own copy of the variable, which is exactly the behavior you're seeing.
To solve this problem, declare the variable in stdafx.cpp
bool MyBool = false;
Then extern it in stdafx.h
extern bool MyBool;
I have a header file that has a number of declarations like this:
extern ID3D10Device* device;
I can't make these static because of a problem with multiple translation units, and I can't have them as normal pointers for a similar reason. However when I try to build the program I get unresolved external symbol linker errors. I'm assuming that this is because I'm attempting to use the pointers without defining them first. This is the problem however, as the way you initialise these DirectX objects is by passing the address of the pointers as parameters into specialist methods. - I may be wrong but I am assuming this is the problem as the compiler / linker / whatever can't see the definitions.
All I'm trying to do is have these pointers (for the graphics device, depth buffer etc) visible to multiple classes. How can this be achieved?
You need the pointers to be defined in some translation unit. The linker is complaining because it seems you haven't done that anywhere. You should declare them at file scope as
ID3D10Device* device = NULL;
in the source file where you call the DirectX function that initializes them. Just make sure the declaration is only made in one source file, then the extern statement should be placed in the associated header file which is included by all translation units that need to use these pointers.
When you externally define a variable like this, the compiler is not reserving any memory for that variable until it sees a definition inside a code module itself. So if you are going to be passing these pointers by-reference to a function for initializing their values, they must be defined in a code module somewhere.
You only need to define them in a single code module ... then place the extern declarations inside a header file you include in the rest of your code modules that require access to the pointer variables. That shouldn't create any linker errors due to duplicate definitions.
I can't make these static because of a problem with multiple translation units
If you need a different variable in different TU (translation units), make it static: this way the variable will be specific to each TU.
A declaration of a variable is also definition unless the extern is used.
You must have one (and only one) definition of a variable in the program.
To have a global variable:
Declare it with the extern keyword in some header file.
Include this header in every TU that needs to use the variable. Never declare the variable directly, never bypass the header inclusion.
Define the variable: a declaration without the extern keyword will define the variable in one TU. You need to include the header file in the same TU to guaranty consistency between the extern declaration and the definition.
the way you initialise these DirectX objects is by passing the address
of the pointers as parameters into specialist methods
To solve this part of the problem, you could do something like this (in a .cpp file):
ID3D10Device* device;
struct Foo {
Foo(ID3D10Device **pdevice) { specialist_method(pdevice); }
};
Foo f(&device);
Beware of the "static initialization order fiasco", though -- it's safe to use device from main, or code called from main, because it will definitely be initialized before that code executes. It's not necessarily safe to use it from other initializers executed before main, because the order of initialization of statics in different translation units is unspecified (within a TU, they're initialized in order of either declaration or definition, I forget which). So device might still be a null pointer in that code. Likewise, specialist_method can't necessarily rely on other statics having been initialized.
There are extra tricks you can use if you need to enforce initialization orders, I'd guess that all the common ones are on SO already in other questions.
I have a number of C++ classes, alot of them (not all) share two "static size variables" e.g.
share.h
/*Other variables in this header used by all classes*/
static size width=10;//Used by about 60%
static size height = 12;//used by about 60%
So I placed them in a header file along with other objects that all classes share.
when I compile the project I get alot of warnings (from the classes which dont use these), which complain about them being defined and not used. But I need them there!
So I ask, is there a way to hash these to prevent such warnings?
Hashing them so that they can be defined! preventing warnings from classes calling this header file which dont require these last two variables, but they call header because they need everything else init
You should place them in an individual header file. So you can include it only in the classes they need it. This avoids the warning in the other classes. So in the end you will have two header files. One where the stuff for all classes is included and another where the variables which are not used in all are defined.
However try to avoid global variables.
Edit reading tune2fs' answer, I realized I may have interpreted the question wrong.
Perhaps you forgot to use extern in the header file? If you just include static definitions in the header file all compilation units will have unique copies, not shared. See also this explanation of static/extern
Edit Disambiguated in comments
static SomeClass NotUnusedInstance;
static void unused_vars_helper()
{
static SomeClass* take_address = &NotUnusedInstance;
}
This approach is design to have minimal impact (not invoking any actual code; take_address isn't actually initialized unless you call unused_vars_helper).
This should work pretty well for your case. You can make unused_vars_helper() static and/or move it inside an anonymous namespace to prevent external visibility of the helper.