speed of static

Started by
7 comments, last by Laroche 21 years, 9 months ago
I was thinking about a certain optimization I could do for my star field in my space game. The way I have it set up is that each star is it''s own entity, and at every frame EVERY star checks the players velocity and modifies its own XY position accordingly. The optimization I was thinking about would make only ONE star check the velocity, store it in a static var, and the other stars would react accordingly. But how is static implemented? Are the other instances going to check that particular instance and set their own vars to the static one? I''m curious about the implementation most compilers use if anybody knows. I use VS 6.
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content
Advertisement
A static member variable is a class-wide global variable...
It''s just like a global variable except its visibility is reduced.

// headerclass Foo{  static int x;};// sourceint Foo::x = 0; // necessary definition in one cpp file. 


The use of static at file scope is deprecated.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan


  //////////////////class.h//////////////////class CDemo{	static int s_x;public:	static void setX(int x);};//////////////////class.cpp//////////////////#include "class.h"//static variable requires you to declare an instance of it...int CDemo::s_x;	//instance of static variablevoid CDemo::setX(int x){	s_x = x;}//////////////////entry.cpp//////////////////#include "class.h"void main(){	CDemo::setX(10);	//call a static function without an instance}  



Hey, I just found out something interesting (on my Borland compiler at least). A static variable in a non-static class function is shared among all the instances of that class! Is this normal?

Firebird Entertainment
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
When we declare data members of a class as static, they are defined only once and are shared between all objects of the class. Each object gets its own copies of each of the ordinary data members of a class, but only one instance of each static data member exists, regardless of how many class objects have been defined.

BAM!

[edited by - Mulligan on June 30, 2002 10:46:46 PM]
quote:Original post by Tron3k
Hey, I just found out something interesting (on my Borland compiler at least). A static variable in a non-static class function is shared among all the instances of that class! Is this normal?

Firebird Entertainment


This makes sense. All instances of a class use the same methods, they just get different non-static data members.

if you look at my previous post, youll see that the instance is instanced at filescope... so it would seem that all class instances would access the same one.
Why not just use the player''s X,Y position as the offset during render? Then you don''t have to change anything.

Changing the values is a huge waste of time. The other thing you could do is at the start of render store the x,y velocity into two temporary vars. Then, before each star is drawn, update their position. That way you only run through the stars once.

Ben


IcarusIndie.com [ The Rabbit Hole | The Labyrinth | DevZone | Gang Wars | The Wall | Hosting ]
Just on static being deprecated at the file scope, instead of that you can use anonymous namespaces which have the same effect.

This topic is closed to new replies.

Advertisement