| re: Does a static variable in a class's member fn always remain static?
"Sam" <hchen@nd.edu> wrote in message
news:f0edac8.0401130651.618ba34@posting.google.com[color=blue]
> I'm thinking about this question. I've a class A and its member
> function fn. In fn there's a static variable s.
>
> If I create an object by
> A a;
>
> When I call the function a.fn, s would remain static throught out all
> the callings of a.fn, in other words it would be affected by all the
> callings of a.fn.
>
> Now the question is, if I've created other objects by
> A b,c,d;
>
> When I call the function b.fn, c.fn, d.fn, will they also affect the
> value of the static s?[/color]
Why don't you try it and see?
The answer is yes. Individual objects have their own copies of a class's
data members, but there is only one copy of each member function and it is
shared by all objects of the class. Thus a.fn(), b.fn(), c.fn(), d.fn() etc.
are all calling the same function containing the same static variable.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead) |