Static var question

Started by
15 comments, last by _Sigma 18 years, 5 months ago
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
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 :)
Oh, ok. cool. So if I checked the list from either obj1 or obj2, it'll contain the same thing eh?
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.
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.
Brilliant. That is so cool :P I never knew that :D
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.

hmmm so

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

would be illegal?
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.
What the hell is 'instance'? :P

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

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

This topic is closed to new replies.

Advertisement