Always.
Static data is data that does not change from program load to program exit. Static data member do not apply for c. In c++, a static data member is one that is common for all instances of that class.
Static data members of a class in namespace scope have external linkage. Static data members follow the usual class access rules, except that they can be initialized in file scope. Static data members and their initializers can access other static private and protected members of their class. The initializer for a static data member is in the scope of the class declaring the member. A static data member can be of any type except for void or void qualified with const or volatile. The declaration of a static data member in the member list of a class is not a definition. The definition of a static data member is equivalent to an external variable definition. You must define the static member outside of the class declaration in namespace scope.
A class can have both static and non-static data. Static data is local to the class while non-static data is local to each object of the class. It makes no difference whether static data is global, local to a file, local to a function or local to a class (whether public, protected or private), all static data is allocated within the program's data segment along with all constants. As such they are allocated at compile time.
A static function, not part of a class, is visible only to other code within the same compilation unit, i.e. the same source file. A static function, part of a class, can only operate on static class data, which is per class data as opposed to per instance data.
Static data members are local to the class in which they are declared. That is, they are shared amongst all instances of the class, unlike instance variables where each instance has its own set of variables. In addition, static data members are also accessible to static member functions, even when no instances of the class actually exist. So if static data members are accessible even when no instance exists, how are we to initialise them? A member function is no use because that would require an instance. And a static member function isn't an option either because then the onus is upon the user to ensure that the method is called BEFORE any instances are created, which completely destroys the encapsulation of the class (not to mention the fact a static member function would require a local static variable in order to determine if it had already been called or not). The simplest solution is to initialise all static data members from outside of the class declaration. Typically we do this from within the class CPP file however we can also do it in the header file, so long as it's not declared within the class declaration. As with all static variables, the initialisation statement is executed at compile time, thus ensuring the member is fully initialised at runtime.
Static data members are different from automatic ones in the way that their lifetime is equals to the lifetime of your program. Even if you have declared static members inside of function (class) other than main();
It may access static data, but you have to know what 'static data' means: data, which is local to the current module (not shared with other modules), so if you use the function both from module 'A' and from module 'B', they will use different variables (with the same name).
initialize static variables are stored in data segment where uninitialized static variables are stored in BSS(block storing for Symbol) it also a part of data segment exp static int i=10;//stored in data segment static int i;//stored in BSS (uninitialized data segment) Thanks NAvin
Local variables (on the stack) or dynamically allocated variables (in the heap) are nonstatic variables. Static variables, constants and globals are all allocated in the program's data segment.
Static function are function only visible to other function in same file Function outside the class can access, useful whenever we need to have functions which are accessible even when the class is not instantiated.
Yes. Static data members are local to the class in which they are declared. Thus all instances of the class share the same variables (unlike non-static data members where each instance of the class has its own set of variables). Moreover, since static data members do not belong to any instance of the class, they are accessible without the need to instantiate an instance of the class, and like all other static variables, remain in scope for the entire duration the program is running. Also, as with all other static variables, they must be initialised at compile time from outside of the class declaration. Usually this is done from the class CPP file.Static member functions are similar to static data members in that they are local to the class, rather than to an instance of the class. Since they do not belong to any instance of the class, they do not inherit an implicit this pointer. As a result, they are accessible without the need to instantiate an instance of the class and will remain in scope for the entire duration the program is running.It is not unusual for a class to have both static data members and static member functions. They can be likened to global variables and global methods, but scoped to the class. However, their visibility can be restricted by the access specifiers enforced upon them (public, protected or private). Although static member functions cannot access instance methods and instance variables (unless an instance is physically passed to them as an argument) they have unrestricted access to the static data members of the class, as do all instances of the class and friends of the class.A classic example of static member functions and static data members in the same class is when one needs to maintain a count of all instances of a class. All the class constructors must increment the static counter while the destructor must decrement it. A static member function such as GetCount() can then report the number of instances currently instantiated, even when there are no instances.There are many other uses, but the golden rule is that they must be related to the class in which they are declared. If their purpose is simply to provide global functionality then declare them as such, or (better) limit their scope by declaring them in a separate class specifically for that purpose with private constructors to prevent any instances from being created (since none would be required).
Basically, it has two major functions: store data to a given address and retrieve data from a given address.