-
declarations of namespace members with static storage duration and internal linkage;
definitions of block scope variables with static storage duration and initialized once;
declarations of class members not bound to specific instances -
static 函数内不能访问非静态成员函数和非静态数据成员,因为它没有this指针,非静态成员函数和非静态数据成员对于具体的实例才有意义
-
static在头文件中修饰全局变量,被多个源文件include后会存在多个内存拷贝; const也具有类似的内部链接属性,但因为编译器常量折叠并不会存在多个内存拷贝。
C++头文件中定义static/const
Defining global constant in C++ -
static在匿名空间是多余的,因为匿名空间本身就具有内部链接属性;static const修饰全局变量和const修饰全局变量效果一样。
类似
this指针
obj - const; print-非const
obj.print(); = print(&obj1);
即把当前的对象obj的this指针传递到print()函数
但静态成员函数可以访问静态成员变量,全局变量,和自身的函数形参。所以可以显式在静态函数中引入了一个对象,然后通过这个对象来无差别权限地访问各个成员函数和变量。
static void A::Instance(A * const pa);
void A::Instance(A * const pa){
pa->func();
}
func()可以是非静态函数