第一部分:关键字static
关键字static可用于声明普通全局变量、普通局部变量、类成员变量、类成员函数。分别如下:
1.1 static 声明全局变量:global static variable
static表明生命周期,全局变量表明作用域。global static variable可被该文件内的所有函数调用,但无法被其他文件调用;当程序结束时,该变量所占用的内存会被释放。global static variable 尽可作用于文件内部,即具备文件作用域(file scope),无法被其他文件调用。static与extern的作用相反。
1.2 static声明局部变量:local static variable
在函数、程序块等中可定义一个local static variable,该变量仅可被该函数或程序块调用。若编译器不对内存进行优化,则该变量可一直存在,直到程序结束。
#include "stdafx.h"
#include <iostream>
int global_value = 5;
void FuncStatic()
{
static int static_value = 10;
static_value += 3;
int local_value = 4;
local_value += 3;
std::cout << "static_value is " << static_value << std::endl;
std::cout << "local_value is " << local_value << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "First call of function FuncStatic!" << std::endl;
FuncStatic();
std::cout << "Second call of function FuncStatic!" << std::endl;
FuncStatic();
return 0;
}
第一次调用:static_value is 13, local_value is 7;
第二次调用:static_value is 16, local _value is 7;
分析可知:第一次调用结束后,local_value占用内存被释放,故第二次调用中得到同样的结果;而static_value所占用内存并没被释放,故第二次调用结果(16)=第一次调用结果(13)+3。
1.3 static 声明类成员变量
类中的静态成员变量属于该类,由该类创建的所有对象共享同一块内存,并不属于某个对象。static variable in class需要注意以下几点:
第一:static variable 不能在声明的同时定义,需要在实现文件(classname.cpp)中单独定义,并且必须在成员函数之外定义。值得一提的是,static const int ,static const char可在声明的时候定义。
第二:普通成员函数可访门静态成员变量及非静态成员变量,静态成员函数只能访门静态成员变量。因为静态成员变量、函数归类所有,而普通成员变量、函数归对象所有。
staticvariable.h文件:
#ifndef CPLUSLEARN_TESTCODE_H_
#define CPLUSLEARN_TESTCODE_H_
#include <string>
class StaticVariable
{
private:
static std::string string_variable_;
static const char char_variable_ = 'c';
static char * string_pointer_;
static const int int_value_= 8;
static int int_value_out_;
static char char_variable_out_;
public:
void SetVale() ;
void SetString() ;
void Show() const;
};
#endif
staticvariable.cpp文件:
#include "stdafx.h"
#include "staticvariable.h"
#include <iostream>
std::string StaticVariable::string_variable_="This is static string";
char * StaticVariable::string_pointer_="This is char array";
int StaticVariable::int_value_out_=8;
char char_variable_out_='c';
/*Modify int value*/
void StaticVariable::SetVale()
{
int_value_out_+=2;
std::cout << "Static value:" << std::endl;
std::cout << "int_value_out_ is " << int_value_out_ << std::endl;
}
/*Modify string*/
void StaticVariable::SetString()
{
string_variable_ += "1";
std::cout << "Static string:" << std::endl;
std::cout << "string_variable_ is " << string_variable_ << std::endl;
}
/*Show many value*/
void StaticVariable::Show() const
{
std::cout << "Show many value:" << std::endl;
std::cout << "char_variable_ is " << char_variable_ << std::endl;
std::cout << "int_value_ is " << int_value_ << std::endl;
}
main.cpp文件:
/*Code section selected*/
std::cout << "Compare static int value in class:" << std::endl;
first_object.SetVale();
second_object.SetVale();
std::cout << "Compare static string in class:" << std::endl;
first_object.SetString();
second_object.SetString();
std::cout << "Show many variable in class:" << std::endl;
first_object.Show();
second_object.Show();
运行结果:
分析结果可知:对于static int及static string变量,first_object 与second_object共用相同的静态变量内存,其存储的变量内容是叠加的。
1.4 static 声明类成员函数
静态成员函数只能调用静态成员变量,普通成员函数可调用静态成员变量及普通成员变量。静态成员函数为类所有,不为某一对象所有。
第二部分:关键字const
const variable为只读书,类成员变量中的const变量必须通过初始化列表定义。默认情况下,const为internal linkage,不能被其他文件调用。使用extern const声明,可使const variable 具备external linkage。补充下:C++中,默认情况下,函数具备external linkage。
第三部分:关键字extern
C++中extern主要有两个作用,其一:外部变量声明;其二:extern "c"告诉编译器使用c的规则处理函数,而不使用C++规则处理。因为C++支持多态重载等,故C++与C的编译方式并不相同。此处只讲述作用一。
默认情况下,函数及全局变量均为external linkage,初始化声明时加不加extern都可以。再次声明时(在其他文件声明),函数可以不加extern,变量必须要加extern。
以下例子中,在one.cpp文件中初始化声明变量,在main.cpp中再次声明并调用。
one.cpp
#include "stdafx.h"
#include <iostream>
/*Variable linkage*/
int global_value;/*Extern linkage, can be modified*/
extern const int extern_const_value = 5;/*Extern linkage, cannot be modified*/
const int const_value = 5;/*Internal linkage, cannot be modified*/
void ShowValue()
{
std::cout << "global_value is " << global_value << std::endl;
std::cout << "extern_contst_value is " << extern_const_value << std::endl;
std::cout << "const_value is " << const_value << std::endl;
}
main.cpp(截取部分片段)
void ShowValue();/*Declaration function ShowValue() in one.cpp*/
extern const int extern_const_value;/*Declaration extern_const_value in one.cpp*/
extern int global_value;/*Declaration global_value in one.cpp*/
void main ()
{
std::cout << "Variable linkage:" << std::endl;
ShowValue();
std::cout << "extern_const_value is :" << extern_const_value << std::endl;
global_value = 5;
std::cout << "global_value is :" << global_value << std::endl;
}
结果如下:
如有错误,欢迎指正!