answersLogoWhite

0

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.

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

Can static member function be overloaded?

Yes. Any function can be overloaded. However you cannot override a static member function. Only instance members can be overridden.


What is a static data linkage?

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.


Can array be initialized if they are static?

Yes.


What is the Use of static function?

A static function is a member function that is not associated with any instance of the class; it has no this pointer.


When Static function can work with static data member only?

Always.


How can a PHP static be initialized?

PHP static can only be initialized using a literal or constant. You can not use an expression. You can initialize it to an integer but you may not to another variable.


What is the difference between static and non static class members in java?

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();


What types of functions cannot be made virtual?

Static member functions, member function templates and constructors cannot be virtual.


What happen when static data field is initialised in a non static constructor?

It got initialized as you instructed.


Which variable is initialized once when program is compiled?

global and static


How you declare class scoped variables and member functions?

To scope class members to the class (rather than to instances of the class), declare them as static members of the class. Static members are accessible even when no instances of the class exist. As such, static member functions do not have access to a 'this' pointer, unlike ordinary (nonstatic) member functions.


Which member should be declared as static?

Members which are shared among all instances of a class should be static.