473,511 Members | 16,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static Vars in member-functions?

Hello.

Is a static Variable in a member function of a class static with regard
to one object, or is it static for all objects of the class?

That is:
Expand|Select|Wrap|Line Numbers
  1. class A {
  2. // ...
  3. void func() {
  4. static int x = 0;
  5. x++;
  6. printf("static x = %d", x);
  7. }
  8. //...
  9. };
  10.  
  11. //...
  12. A obj1;
  13. B obj1;
  14.  
  15. obj1.func(); // should print 1
  16. obj2.func(); // does this print 1 or 2 ??
  17.  
best regards,
Martin

Jul 23 '05 #1
2 1088
Martin T. wrote:
Hello.

Is a static Variable in a member function of a class static with regard
to one object, or is it static for all objects of the class?
What does that mean?
Anyway, a static variable in a member function is just the same as a static
variable in any other function. It is initialized when the function is
first called, then re-used in subsequent calls.
That is:
Expand|Select|Wrap|Line Numbers
  1.  class A {
  2.   // ...
  3.   void func() {
  4.     static int x = 0;
  5.     x++;
  6.     printf("static x = %d", x);
  7.   }
  8.   //...
  9.  };
  10.  //...
  11.  A obj1;
  12.  B obj1;
  13.  obj1.func(); // should print 1
  14.  obj2.func(); // does this print 1 or 2 ??
  15.  


2
Jul 23 '05 #2
Rolf Magnus wrote:
Martin T. wrote:
That is:
Expand|Select|Wrap|Line Numbers
  1.  > class A {
  2.  >  // ...
  3.  >  void func() {
  4.  >    static int x = 0;
  5.  >    x++;
  6.  >    printf("static x = %d", x);
  7.  >  }
  8.  >  //...
  9.  > };
  10.  >
  11.  > //...
  12.  > A obj1;
  13.  > B obj1;
  14.  >
  15.  > obj1.func(); // should print 1
  16.  > obj2.func(); // does this print 1 or 2 ??
  17.  > 


2


Thanks

Jul 23 '05 #3

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

Similar topics

3
1243
by: Christopher J. Bottaro | last post by:
Hello, Lets say I have a class instance with the following member vars: var1, x, size. Now lets say I have dict with the following keys: var1, x, size. Is there an easy way to *automatically*...
9
2475
by: vp | last post by:
Can I safely assume that all static variables are initialized as NULL or zero, depending on the types of the variables, no matter on which platform that app is compiled ? Thanks for your help, ...
4
273
by: JM | last post by:
Hi all, I have a Customers class that can be instantiated with a customerId , which causes private member vars to be populated. But I also want to simply make a call to a static method on the...
1
1538
by: Harald | last post by:
Hi My question: I use a few static vars in my site and want to know if their values interfere between ore than one session. Sample: First session: static int x = 10 Second session: static int...
3
7316
by: Dave | last post by:
Are there any issues with concurrency on static methods creating instances such as in the data access code below? Or if I wanted to expand it to use an ADO.NET transaction and having two users...
2
1365
by: r.roest | last post by:
Hi, I'm looking for a way to fill static vars from a different c-file. Here is what I want to do: file1.c static DEBUG calling function X using DEBUG to display information calling...
6
1748
by: Bran Kelly | last post by:
Hi, I am running into something I haven't ever encountered before. I have a static instance of a class declared in a cpp file, which seems to be optimized (or for some other reason) out of...
10
1744
by: John A Grandy | last post by:
Say I have Class1 which contains static Class2 var1 = new Class2(); Is Class2 constructor code only executed if var1 is referenced in the code-execution path ? Or is Class2 constructor code...
55
6151
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...
6
1171
by: RishiD | last post by:
Hi, I am trying to figure out if the local variables created within a static member function are created on the heap or stack? Example: class Circle {
0
7242
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
7355
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
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
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
5668
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,...
0
4737
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...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
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...

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.