473,785 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1411
"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.googl e.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.googl e.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
2988
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
4897
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 (unresolved external symbol) Below is what an example of the code I use. Can somebody tell me what I am doing wrong ? Thanks very much in advance, Serge //myclass.h
3
4608
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 the compiler generates a copy of class Include one each for test1.cpp and test2.cpp. But however they use the same class definition for both test1.cpp and test2.cpp.
10
2909
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 SomeClass { public SomeClass() { abc++; // Instance Variable xyz++; // Static Variable
12
2586
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 advance, Joe
5
4717
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 static char szMTreeBuf, it can work again. But when add some other function codes, it cannot run normally. I modify the class delearation, and change szMTreeBuf member variable to be local variable. Again it can run now. Why does C++ program run...
5
11326
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 always optimize and turn it into a static const variable to save runtim memory/stack space since being const it cannot be changed. (?) Thanks,
55
6249
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 C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
9
8895
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 *defined* "x"'s value to be 10, isn't above statement a
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9485
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10098
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.