static

closed account (SECMoG1T)
Hi , av used static keyword before and I have worked with static variables from time to time but I have just realized that I don't really understand what static means, please help me clear this out in my head, thanks in advance
1
2
3
4
5
6
7
8
9
10
11
12
   int count1; /// a global variable
  struct account
   {
     static interest rate; /// all objects of struct will share this single v
                           /// variable ?
     double balance; 
     double credit; 
     double bonus; 
   }

   static int count;  /// what about this local variable?
   /// whats the diffence between count and count one  
Last edited on
In my opinion, static useful when u working with classes.
Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

class Parent
{
public:
static int StaticX;
};

class Child1: public Parent
{
int X;
};

class Child2: public Parent
{
int Y;
};

void main ( void )
{
Child1 MyChild1;
Child2 MyChild2; 
}

In this listing, the StaticX will have only ONE exemplar. For example, StaticX have adress 0x00000001.
So in MyChild1 and MyChild2 this variable will have 0x00000001 adress. So u saving some memory, using "static".
1
2
3
// my_file.cpp
int count1;
static int count;


count is only visible within the translation unit of my_file. This allows the compiler to be able to make more specific choices about how it uses count. count1 is not specific because it may be visible to other files.

static interest rate; /// all objects of struct will share this single variable

Yes. Say you had a bunch of accounts that should all have the same interest rate. If you decide to change the interest rate, it is simpler to do it once.

Another use might be if you had bunnies that had a list of names to choose from:

1
2
3
4
5
6
7
8
9
struct Bunny
{
  static const std::string names[];
  const int name_id;

  Bunny( void ) : name_id( getRandomNumberLessThanNamesLength() ) {}
};

const std::string Bunny::names = { "Thumper", "Roger", "Bugz" };


Static variables are only initialized once, which can be more effecient:
1
2
3
4
5
6
7
8
int highestPrime( void ){  /* calculates and returns a high prime number */ }

void usePrime( void )
{
  static int prime = highestPrime();  // highestPrime, an expensive function, only gets called once

  // do things with prime
}
Last edited on
closed account (SECMoG1T)
Thanks for that so static should be used only in classes ,??
static means slight different things in different places, but in all cases, it is similar to a global variable in the sense that it exists for the lifetime of the program.

Myfile.cpp:
1
2
3
4
5
6
7
8
9
10
11
static int fileStatic;

class MyClass {
    static int classStatic;
};
int MyClass::classStatic;

void func()
{
    static int funcStatic;
};


Here fileStatic, classStatic and funcStatic are all single variables. There is exactly one of each. They last for for the lifetime of the program. The only difference is in who can see them.

fileStatic is visible only to code within MyFile.cpp

classStatic is within the name space of MyClass (apologies for not having the terminology right here. I hope someone will correct me). classStatic's depends on whether it is private, protected or public.

funcStatic is only visible within func().

In summary, the difference between global and static variables is their visibility.

[There is one subtle difference: function statics are initialized the first time the function is called rather than before main() runs.]
closed account (SECMoG1T)
Thank @lowest0ne and @Divergence for that I appreciate you have given me some important highlights
But hey, so plain local static variables are better not declared static coz they are equivalent to their non static counterparts
No, plain local static variables serve a useful function by being hidden from the rest of the program.
Topic archived. No new replies allowed.