Archived

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

EvilCrap

Is this standard c++ ?

Recommended Posts

for you c++ gurus, could u clarify this for me? On the .net compiler, a static base class member is shared by derived classes, but if i recall, on the mvc6 compiler, derived classes had their own instance of static variables. which is standard? for instance:
class CBase{
public:
static int i;
};
int CBase::i=0;
class CDerA: public CBase{
public:
 void p(){ cout << i++;}
};
class CDerB: public CBase{
public:
 void p(){ cout << i++;}
};
void main(){
 CDerA a;
 a.p(); a.p();
 CDerB b;
 b.p();
}
on .net compiler result is "012" is this an unstandard behavior i should avoid or is it proper?

Share this post


Link to post
Share on other sites
A static variable in a base class won''t be reinstantiated in derived classes. They''ll all refer to the base class. This was also the case in MSVC6, IIRC.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites