TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: All
from: Neil Heller
date: 2003-09-02 14:32:00
subject: Static vars in functions

If there is a static variable in a methodology of a class, does that 
variable get re-initialized with each new instance of the class?

for example, suppose I had a class with one method:

file  foo.h

class foo
{
     public:
          void SomeFunc();
};


file foo.cpp

void foo::SomeFunc()
{
     static int IntVar = 0;

     IntVar++;
}


file AnApp.cpp

#uses foo.h

for (counter = 0; counter < SomeOtherVar; counter++) {
     if (SomeVar) {
          foo * pfoo = new foo();
          pfoo->SomeFunc();
          delete pfoo;
     }
     DoSomethingElse();
}

Does IntVar in pfoo->SomeFunc() get reinitialized to zero with each new 
instance of class foo or is it initialized only once per application?

þ CMPQwk 1.42 999

--- Maximus/2 3.01
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)
SEEN-BY: 633/267 270
@PATH: 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.