Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Does a static variable in a class's member fn always remain static?

Reply
Thread Tools

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

 
 
Sam
Guest
Posts: n/a
 
      01-13-2004
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?
 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      01-13-2004
"Sam" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)
> 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.


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

 
Reply With Quote
 
 
 
 
Derek
Guest
Posts: n/a
 
      01-13-2004
"Sam" wrote:
> 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.


 
Reply With Quote
 
Sumit Rajan
Guest
Posts: n/a
 
      01-13-2004

"Sam" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ...
> 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.


 
Reply With Quote
 
The Directive
Guest
Posts: n/a
 
      01-13-2004
[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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wifi Routers: Does Channel SIX remain on at all times? TBerk Wireless Networking 3 04-22-2008 09:47 PM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
why does this tag always remain hidden? Bob ASP .Net 2 06-15-2006 02:04 PM
WHY does Microsoft allow IE to remain so unpatched? Au79 Computer Support 0 03-03-2006 07:00 AM
GUID in ASP - defined variable does not remain constant BigDadyWeaver ASP General 6 02-03-2004 01:19 PM



Advertisments