Does a static variable in a class's member fn always remain static?

S

Sam

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?
 
J

John Carson

Sam said:
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?


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.
 
D

Derek

Sam said:
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?

Yes.
 
S

Sumit Rajan

Sam said:
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?

Why don't you try it out?

#include <iostream>

class X {
int i;
public:
X(){i=0;};
int fn();
};

int X::fn()
{
static int s = 0;
return ++s;
}

int main()
{

X a;
std::cout << a.fn() << '\n';
X b;
std::cout << b.fn() << '\n';
std::cout << a.fn() << '\n';
}


Does that answer your question?
The static variable, s, retains its value between calls.

Sumit.
 
T

The Directive

[Snipped]
When I call the function b.fn, c.fn, d.fn, will they also affect the
value of the static s?

Absolutely yes (A simple test program would have given you the answer.)

--The Directive
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top