static question

Started by
3 comments, last by nobodynews 16 years, 6 months ago
Okay, I've got a game code (for Win32) from a book. The code compiles fine but something's off: the WinMain function declares a static variable! Like this: int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static int iTickTrigger = 0; .................. } Any idea why he put "static" there? Thanks
Advertisement
Erm.... no, not really. I could think of a couple ways this would be needed, but both of then are bad ideas anyway. I'm guessing that this variable was global and he forgot to remove static from it.

I'd also like to say: Please don't use Hungarian notation like he does.
Why not Hungarian Notation? What should he be using then as a standard naming convention? I assume you dont simply expect people to make up their own naming conventions...
Quote:Original post by HumanoidTyphoon
Why not Hungarian Notation? What should he be using then as a standard naming convention? I assume you dont simply expect people to make up their own naming conventions...


There's an interesting approach which consists in naming the object after its semantic content, not its type. For instance, iTickTrigger tells you that it's an integer that somehow triggers a tick, or is triggered by a tick, or something else—the meaning of the variable is not really clear from its name. A better name, such as msUntilNextTick tells you that you're counting the milliseconds until the next tick happens—how the milliseconds are counted (integer? floating-point? something else?) is not as relevant as the fact that these milliseconds represent a delay before an event.
Should you find yourself in a situation where you want to know the type of the variable you can often just hover over the variable in some (several?) IDEs such as in MS Visual Studio which will bring up a box detailing the type. You can also right click on the variable and click on the "go to declaration" option to be taken directly to the correct line of code.

As such, you know longer really need embed type information into a variable, except perhaps some rare situation.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement