Info: running QNX RTP, compiling with qcc.
If a variable is declared static inside a method of a class, would you
expect it to be local static to the method or class static? The reason I am
asking is because a static variable in a method of a class that I was using
behaved like a class static variable. When I declared multiple instances of
the class both of them were using the same copy of the static variable. Is
this what I should expect?
“Kevin Hykin” <kevin.hykin@bepco.com> wrote in message
news:9jmikb$61e$1@inn.qnx.com…
Info: running QNX RTP, compiling with qcc.
If a variable is declared static inside a method of a class, would you
expect it to be local static to the method or class static? The reason I
am
asking is because a static variable in a method of a class that I was
using
behaved like a class static variable. When I declared multiple instances
of
the class both of them were using the same copy of the static variable.
Is
this what I should expect?
Yes
This is probably a better question for comp.lang.c++. As far as I can tell,
all that is stated by the specification is that in c++, declaring a class
variable as static causes all instances of the class to share that variable.
It doesn’t seem to make any distinction between a variable declared in class
or method scope. So, that would mean that the behaviour you are observing
is correct.
cheers,
Kris
Kevin Hykin <kevin.hykin@bepco.com> wrote:
Info: running QNX RTP, compiling with qcc.
If a variable is declared static inside a method of a class, would you
expect it to be local static to the method or class static? The reason I am
asking is because a static variable in a method of a class that I was using
behaved like a class static variable. When I declared multiple instances of
the class both of them were using the same copy of the static variable. Is
this what I should expect?
–
Kris Warkentin
kewarken@qnx.com
(613)591-0836 x9368
“You’re bound to be unhappy if you optimize everything” - Donald Knuth
Kevin Hykin <kevin.hykin@bepco.com> wrote:
Info: running QNX RTP, compiling with qcc.
If a variable is declared static inside a method of a class, would you
expect it to be local static to the method or class static? The reason I am
asking is because a static variable in a method of a class that I was using
behaved like a class static variable. When I declared multiple instances of
the class both of them were using the same copy of the static variable. Is
this what I should expect?
Check out http://www.calpoly.edu/~jfischer/c++faq/defs/static_variable.html
That should explain all the details you need.
-Adam