Jump to content
  • Advertisement
Sign in to follow this  
_Sigma

Static var question

This topic is 4706 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've been reading the Enginuity articles (amazing btw :D) and I have a quick, really dumb question. I've never dealt with static variables in a class before, so if you have a class like thus:
class IMMObject
{
   private:
      static std::list<IMMObject *> liveObjects;
      statid std::list<IMMObject *> deadObjects;
      long refCount;
   protected:
      IMMObject();
      ~IMMObject();
   public:
      void AddRef();
      void Release();
      static void CollectGarbage();
};

std::list<IMMObject *> IMMObject::deadObjects;

void IMMObject::Release()
{
   --refCount;
   if(refCount<=0)
   {
      liveObjects.remove(this);
      deadObjects.push_back(this);
   }
}

void IMMObject::CollectGarbage()
{
   for(std::list<IMMObject *>::iterator it=deadObjects.begin();
       it!=deadObjects.end(); it++)
   {
      delete (*it);
   }
   deadObjects.clear();
}

IMMObject::IMMObject()
{
   liveObjects.push_back(this);

   //update the constructor to initialise refCount to zero
   refCount=0;
}

Any class that inherites or uses IMMObject will get the same copy of liveObjects and deadObjects right? So:
IMMObject obj1;
liveObjects will contain obj1. now lets say I go:
IMMObject obj2;
then liveObjects will contain obj1 and obj2, right? Also, why is a method static? whats the point in this? Cheers

Share this post


Link to post
Share on other sites
Advertisement
correct ...

and the point of a static method is to be able to call a function (which probably manipulates static data) without having created an instance ...

For instance you could call something like

int count = IMMObject::LiveObjectCount();

to reach the size of the list without having to create an IMMObject just to get the count :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Xai
correct ...

and the point of a static method is to be able to call a function (which probably manipulates static data) without having created an instance ...

For instance you could call something like

int count = IMMObject::LiveObjectCount();

to reach the size of the list without having to create an IMMObject just to get the count :)


Additionally, static member functions follow class permissions (public, protected, private). So you cannot access liveObjects or deadObjects, but CollectGarbage() can.


jfl.

Share this post


Link to post
Share on other sites
Quote:
Original post by _Sigma
Oh, ok. cool. So if I checked the list from either obj1 or obj2, it'll contain the same thing eh?


Yes.

Share this post


Link to post
Share on other sites
Quote:
Original post by Xaiand the point of a static method is to be able to call a function (which probably manipulates static data) without having created an instance ...


Also, do note that by that token, you cannot directly access non-static member variables/functions from a static member function.


jfl.

Share this post


Link to post
Share on other sites
hmmm so


class blah
{
private:
int a;
static int b;

public:
static int returnAB()
{
return a*b;
}

};

would be illegal?

Share this post


Link to post
Share on other sites
Yes.

Now what you can do is the following:

class test {
private:
int a;
static int b;

public:
test( int a ) : a( a ) {}

static int returnAB( const test &instance ) {
return instance.a * b;
}
};
int test::b = 20;


int main() {
test t( 2 );
test::returnAB( t ); // returns 40
}

[edit] Now, alone, this may not seem particularly useful -- since you are in fact imitating a non-static member function -- but it can be useful in cases like this, where you would like to use a non-static method as a callback (for instance).


jfl.

Share this post


Link to post
Share on other sites
What the hell is 'instance'? :P

and why is it
test::returnAB( t ); // returns 40

Can it be
test.returnAB( t ); // returns 40,
? cheers!

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!