Static as substitute of private members.

Started by
18 comments, last by SeanMiddleditch 10 years ago

The C way to deal with variables that are not supposed to be used by client programmers is by using the static keyword in global space. In this way the variables are not available to external files, so that, even if an implementation file imports the header that contains such private variables, they are unavailable (name-definition-unaware, or more technically: out of scope).

If I can recall correctly, extern variables are declared in the header and defined in the implementation file.

What disadvantages are there from e.g. C++'s private class members?

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.
Advertisement

Static members are global, global is bad.

But the main problem is that static members are not instance members. You decide to use static when there will only be one member for all instances of that class globally, not based on visible scope.

Static is not a replacement for private in C. Declaring and defining them only in the .C file is.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Static members are global, global is bad.

But the main problem is that static members are not instance members. You decide to use static when there will only be one member for all instances of that class globally, not based on visible scope.

Static is not a replacement for private in C. Declaring and defining them only in the .C file is.

L. Spiro

Why static is bad ? In an engine class it's good to have all init using Init() function.

You use this way on your engine : http://lspiroengine.com/docs/classlse_1_1_c_engine.html

On a renderer it's nice to have RenderState class with static variables + Init() to avoid Renderer.cpp of 4000 lines.

Do you think struct + extern is better to use than class/struct with static ?

I really want your opinion about all that L. Spiro smile.png.

Why static is bad ? In an engine class it's good to have all init using Init() function.


It's non-ideal, not bad. There are a lot of problems with it, however. It makes threading a pain in the butt. It makes writing tests against mock objects a pain in the butt. It makes some kinds of debugging a pain in the butt.

On a renderer it's nice to have RenderState class with static variables + Init() to avoid Renderer.cpp of 4000 lines.


That doesn't follow. Nothing about static variables makes the rest of your code smaller. You've got a false equivalency going on here.

Do you think struct + extern is better to use than class/struct with static ?


I don't understand what these have to do with each other.

Make functions or classes that take as parameters any dependencies they have and don't rely on statics. This is called Dependency Injection, and is something to strive for. That way, for instance, you can pass in a NullaryRenderDevice or something so you can test your renderer in a unit test on your build machine without needing an actual GPU, or a MockFileSystem that serves up only some cooked-in test files for tests. It lets you switch out things at runtime easily.

There are many places for static values. I use them extensively in reflection code, error dialog and assert code, memory space support, and so on. They're all hidden behind very special-case features and not part of the main game code or even the main game systems.

Everything else are systems/manager encapsulated in classes that require references to interfaces for other systems. e.g the GraphicsSystem requires a reference to IResourceSystem so that it can load textures and such on demand. The AudioSpace attached to any Space on the GameObjectSystem has a reference to an IAudioSystem so that sound sources in game can play sound. etc. No statics, no globals, no singletons, no service locator, none of that cruft. I can initialize the engine, systems, or spaces in any configuration I need, including initializing them completely differently in the release mode game, debug mode game, editor, asset baker, multiplayer server, etc.

Sean Middleditch – Game Systems Engineer – Join my team!


On a renderer it's nice to have RenderState class with static variables + Init() to avoid Renderer.cpp of 4000 lines.

Huh? How does a static Init avoid 4000 lines?

There's really no good reason to have your renderer or engine be static/global.

I explain my use of statics here: http://lspiroengine.com/?p=570
And because I dislike statics and tend to avoid them myself, anyone who disagrees with my use there is perfectly in the right. A graphics module doesn’t need to be global, but I also strongly dislike having to pass an instance down to every graphics resource such as a vertex buffer. I also disagree with a graphics object that spits out vertex buffers, textures, etc., which would solve that problem, but breaks the single-responsibility rule in my opinion; I prefer a vertex buffer/texture/index buffer/shader/etc. that can simply be made just like any other object and it knows by itself how to allocate its resources and activate them.

All that being said, I finally decided to use static for that small section of the engine because statics are not always evil, you just have to use them responsibly.
http://www.gamedev.net/topic/647440-why-are-static-variables-bad/
The downsides of statics are explained there and above.
As I use them sparingly and after much consideration, I do not have a problem with any of these downsides.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

A graphics module doesn’t need to be global, but I also strongly dislike having to pass an instance down to every graphics resource such as a vertex buffer.


I solve this by making my graphics resources opaque objects. Only things that manipulate resources need the graphics system, which turns out to be relatively rare (it's mostly the code that generates the draw commands). It's fairly D3D11-ish. As a further bonus, all my graphics resource handles are C++ wrappers around ints, making it much easier to sort by material/buffer for rendering (especially as I guarantee that the unique bits of those integers are the lower X bits, meaning you can radix sort on only X bits instead of on most of the bits of a pointer).

Sean Middleditch – Game Systems Engineer – Join my team!

As a side note, the actual use of C-style static in global space has really been replaced by anonymous namespaces now and static in global space in C++ is really only for backwards compatibility with legacy C code as far as I'm aware. I prefer anonymous namespaces because its too easy to forget to add static to the front of a new definition but easy to remember to include it in the namespace.

I don't use much, if any, static global state but I tend to put implementation functions in anonymous namespaces to avoid accidental linking and name clashes.


Static is not a replacement for private in C. Declaring and defining them only in the .C file is.

Don't they need to be declared static inside the .C file so that they only visible from within that compilation unit.


Static is not a replacement for private in C. Declaring and defining them only in the .C file is.

Don't they need to be declared static inside the .C file so that they only visible from within that compilation unit.

No, each compilation unit is compiled individually, a variable has to be declared as extern to become accessible across compilation units.

Edit: Sorry, obviously incorrect.

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement