static simply means the object has properties of a global, but has limited scope depending on where it is defined at.
Not that hard.
static data members declare the object as a global, but limited to the scope of the class. Hence, all objects of the class can access it.
Code: Select all
class a {
public:
static int m_data; // has properties of a global.
};
// As it is limited to the scope of the class definition, only objects of this
// type can access the variable.
// Because it is a global variable, we need to give it a definite presence
// by defining it globally.
int a::m_data=0; // the global object is actually defined here.
static member functions are the same way. Declaring a member function as static is similar to declaring a non member function as static. Under both instances, they have the properties of a global function.
In the case of a member function, the function is globally defined, but has limited scope to the class. It works the same way data members do:
Code: Select all
class a{
public:
static void foo (); // this function had the properties of a global function
// but is limited to the scope of the class definition.
};
// Because the function is global, we need to define it globally:
void a::foo () {
}
Under both data members and member functions, all objects of the class can access this data and routine as if they were globally defined. The reason is because they are--They just have limited scope.
static Local variables are the same way. As they have properties of global variables, they are not destroyed on function exit.
If you static on global variables or functions, it means they have file scope. That doesn't make sense at all.
Yes it does. static data are meant as another way of specifying data and routines with properties of globals, not external symbols.
Yep, the difficult part isn't actually understanding it, but remembering where it means what.
Under every situation listed here, static has always meant the same thing: It declares data or routines with the properties of a global, inside of limited scope. It does not mean anything different.