top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the use of static variable in a C++ class?

+3 votes
469 views

Please provide some details on the static class variable, how it works and any example would be great.

posted Dec 6, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+6 votes

In C++, When a static variable is declared inside a class it is known as class variable. This static variable is shared among all the objects of that class. If a programmer want to track the number of objects of a particular class in the system, such variables helps.
It's a long time back when I studied so I could not provide much details.
Looking for others to respond.

answer Dec 6, 2013 by Vimal Kumar Mishra
Similar Questions
+5 votes

You can find three principal uses for the static. This is a good way how to start your answer to this question. Let’s look at the three principal uses:

  1. Firstly, when you declare inside of a function: This retains the value between function calls

  2. Secondly, when it is declared for the function name: By default function is extern..therefore it will be visible out of different files if the function declaration is set as static..it will be invisible for outer files

  3. And lastly, static for global parameters: By default you can use global variables from outside files When it’s static global..this variable will be limited to within the file.

is this right ?

+3 votes

As per my understanding uninitialized static variable is stored in BSS and initialized static variable is stored in DS, then what about the variable which is initialized with 0 ? where it will be stored BSS/DS?

...