Advertisement Jump to content
Sign in to follow this  
gasto

Static as substitute of private members.

This topic is 1741 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Edited by Alundra

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

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.

Edited by Aardvajk

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites

 


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.

Edited by SimonForsman

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!