class A { void MyFunc() { static int i = 0; i++; }
A MyA1;
A MyA2;
MyA1.MyFunc(); // i = 1
MyA2.MyFunc(); // i = 2
//
MyA1.MyFunc(); // i = 1
MyA2.MyFunc(); // i = 1
Static variables in functions
Ok, I'm having problems and I was wondering if static variables in functions span across classes:
My problem is that I have a static float that contains the elapsed time over several seconds (for timing.) In my character class I have an anim instance. I then have 2 character instances. Will both classes use the same static float? I'm assuming so.
Thanks!
Yes, a static local variable will span across all instances. There is only ONE such variable regardless of how many objects you instanciate. If that's inconvenient to you, use an actual class member (and not a static member either...)
As a curiosity, in VB .Net they are implemented behind the scenes as instance members, and as such they are unique per class instance.
C++ probably implements them as static class members.
C++ probably implements them as static class members.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement