answersLogoWhite

0


Want this question answered?

Be notified when an answer is posted

Add your answer:

Earn +20 pts
Q: Why you define static data member in global declaration?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the difference between static function and global static function?

'global static'?! There is no such thing.


What is the difference between static data member and ordinary data member?

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.


Why static variables in a function have global extent?

A variable declared static outside of a function has a scope the of the source file only. It is not a global variable. A variable declared outside of a function and without the static qualifier would be a global variable.


When do you declare a member of a class static?

Static members should be used whenever you have a member that is logically considered part of the class, but is not associated with any instance of the class. They are bit like global variables and functions but there's more control over their visibility outside of the class.Static member methods can be thought of as being global functions that are scoped to the class (rather than to each instance of the class). As such, they do not inherit an implicit this pointer and can therefore be called without instantiating an instance of the class (access specifier permitting). However, any and all instances of the class have unrestricted access to all the static member methods of the class.Static member variables can be thought of as being global variables that are scoped to the class (rather than to each instance of the class). That is, all instances of the class share the same set of static member variables amongst them, rather than each having their own (as they would with non-static member variables). Static member variables are also accessible to static member functions. As with all other static variables, they are initialised at compile time (which must be done outside of the class declaration), and will remain in scope for the entire lifetime of the program.All static members are subject to the public, protected or private access specifiers, so while they can act like global variables and functions if declared public, their visibility outside of the class can be limited to private or protected access.Possible uses for static members are many and varied. By way of an example, suppose you want to maintain a read-only count of all instances of a class. The following framework demonstrates how this can be done with static members:class Object{public:Object(){++s_instances;}~Object(){--s_instances;}static unsigned int GetInstances(){return(s_instances);}private:static unsigned int s_instances;};// Static member variable initialiser (evaluated at compile time):unsigned int Object::s_instances=0;int main(){cout


Does the order of public and static declaration matter in main method?

No

Related questions

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.


What is the difference between static function and global static function?

'global static'?! There is no such thing.


Is it possible to write in static variables in main method in java?

Short answer: No. Only class member variables may be declared static. Local variables with a static declaration will throw an error (usually "illegal start of expression").


What is the difference between static data member and ordinary data member?

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.


Can you use this operator to refer static variable?

No. A static member variable is local to the class in which it is declared (much like a global, but scoped to the class) and is accessible to all instances of the class. Since it does not belong to any one instance of the class, it cannot be accessed via the this pointer, as you can with non-static members. Implicitly accessing the variable is the same as explicitly accessing it via ::.Note that it is possible to access a static member variable from outside the class by providing an accessor (getter) for it within the class. The accessor should be static as well, but needn't be, but it should return by value, otherwise it is no better than a global.


Why static data members are not initialized in member function?

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.


Why static variables in a function have global extent?

A variable declared static outside of a function has a scope the of the source file only. It is not a global variable. A variable declared outside of a function and without the static qualifier would be a global variable.


When do you declare a member of a class static?

Static members should be used whenever you have a member that is logically considered part of the class, but is not associated with any instance of the class. They are bit like global variables and functions but there's more control over their visibility outside of the class.Static member methods can be thought of as being global functions that are scoped to the class (rather than to each instance of the class). As such, they do not inherit an implicit this pointer and can therefore be called without instantiating an instance of the class (access specifier permitting). However, any and all instances of the class have unrestricted access to all the static member methods of the class.Static member variables can be thought of as being global variables that are scoped to the class (rather than to each instance of the class). That is, all instances of the class share the same set of static member variables amongst them, rather than each having their own (as they would with non-static member variables). Static member variables are also accessible to static member functions. As with all other static variables, they are initialised at compile time (which must be done outside of the class declaration), and will remain in scope for the entire lifetime of the program.All static members are subject to the public, protected or private access specifiers, so while they can act like global variables and functions if declared public, their visibility outside of the class can be limited to private or protected access.Possible uses for static members are many and varied. By way of an example, suppose you want to maintain a read-only count of all instances of a class. The following framework demonstrates how this can be done with static members:class Object{public:Object(){++s_instances;}~Object(){--s_instances;}static unsigned int GetInstances(){return(s_instances);}private:static unsigned int s_instances;};// Static member variable initialiser (evaluated at compile time):unsigned int Object::s_instances=0;int main(){cout


When Static function can work with static data member only?

Always.


What is the difference between a static variable a global variable and a local variable?

A static variable is a variable allocated in static storage. A local variable is a variable declared inside a function. A global variable is a variable declared outside of any class or function. Note that local variables and global variables can both be allocated in static storage.


Does the order of public and static declaration matter in main method?

No


How do you declare the member of class static?

by the help of static keword in second statment.