473,671 Members | 2,426 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 1095
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
1253
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* assign all the values in the dict to corresponding (member) vars of the same name as the dict keys? Thanks for the help.
9
2497
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, DT
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 Customers class to get shipping addresses, avoiding the overhead of instantiating the class (with it's SQL customer lookup) Customers.GetShippingAddresses(customerId here's the signature internal static DataSet GetShippingAddresses(int customerId...
1
1547
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 x = 5 If a third session is started does the value of my static remain its value in all sessions, or is the value the same in all sessions. I know that this sounds a bit silly but i have problems which look like that is the fault.
3
7329
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 access this method at the same time? As I understand it, it would not be an issue because the variables being referenced aren't static fields and are either created inside the method or passed in. In other words is there any real difference...
2
1381
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 function Y using DEBUG to display information
6
1753
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 existence. It is something like the following: foobar.hpp class foobar {
10
1752
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 executed on load of any class that references Class1 ?
55
6206
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
6
1178
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
8924
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...
0
8823
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8672
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6234
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
5702
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
4227
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
4412
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1814
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.