473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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?
Jul 22 '05 #1
4 1392
"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.
Jul 22 '05 #2

"Sam" <hc***@nd.edu> wrote in message
news:f0************************@posting.google.com ...
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.
Jul 22 '05 #3
"Sam" <hc***@nd.edu> wrote in message
news:f0************************@posting.google.com
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)

Jul 22 '05 #4
[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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Eric | last post by:
I have a static class member variable as follows: struct A { static void Set (int i) { v = i; } static int& Get () { return v; } static int v; }; int A::v; // define A::v in the cpp file
4
by: Serge | last post by:
Hi, I have no problem creating a static member variable with integers, etc but when I try the same with a vector then I always get linker errors that the static member variable is unknown...
3
by: ishekara | last post by:
Hi, Can you solve the puzzle for me? I have a main.cpp, testinclude.h, and test1.cpp and test2.cpp which include testinclude.h testinclude.h has a class Include defined. I always thought that...
10
by: Rene | last post by:
I jus realized that I can change the values of "static variables" and "instance variable" through the standard constructor This means that something like this will compile: public class...
12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
5
by: Allen | last post by:
I wrote many classes. In a class, there is a member variable which is declared as char szMTreeBuf. On both Windows XP and VxWorks, it cannot work. Then I try to declare the member variable as...
5
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.