• Advertisement

Archived

This topic is now archived and is closed to further replies.

speed of static

This topic is 5778 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

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.

Share this post


Link to post
Share on other sites
Advertisement
A static member variable is a class-wide global variable...
It''s just like a global variable except its visibility is reduced.


// header
class Foo
{
static int x;
};

// source
int 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 ]

Share this post


Link to post
Share on other sites


  
//////////////////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 variable

void CDemo::setX(int x){
s_x = x;
}
//////////////////entry.cpp//////////////////

#include "class.h"
void main()
{
CDemo::setX(10); //call a static function without an instance

}



Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Just on static being deprecated at the file scope, instead of that you can use anonymous namespaces which have the same effect.

Share this post


Link to post
Share on other sites

  • Advertisement