随笔-生活工作点滴

c++ static关键字

2019-07-07  本文已影响3人  王王王王王景

static关键字是C, C++中都存在的关键字, 它主要有三种使用方式, 其中前两种只指在C语言中使用, 第三种在C++中使用(C,C++中具体细微操作不尽相同, 本文以C++为准).
(1)局部静态变量
(2)外部静态变量/函数
(3)静态数据成员/成员函数

一、局部静态变量

在C/C++中, 局部变量按照存储形式可分为三种auto, static, register
与auto类型(普通)局部变量相比, static局部变量有三点不同

  1. 存储空间分配不同
    auto类型分配在栈上, 属于动态存储类别, 占动态存储区空间, 函数调用结束后自动释放, 而static分配在静态存储区, 在程序整个运行期间都不释放. 两者之间的作用域相同, 但生存期不同.
  2. static局部变量在所处模块在初次运行时进行初始化工作, 且只操作一次
  3. 对于局部静态变量, 如果不赋初值, 编译期会自动赋初值0或空字符, 而auto类型的初值是不确定的. (对于C++中的class对象例外, class的对象实例如果不初始化, 则会自动调用默认构造函数, 不管是否是static类型)
    特点: static局部变量的”记忆性”与生存期的”全局性”
    所谓”记忆性”是指在两次函数调用时, 在第二次调用进入时, 能保持第一次调用退出时的值.
#include <iostream>

using namespace std;

void staticLocalVar()
{
     static int a = 0; // 运行期时初始化一次, 下次再调用时, 不进行初始化工作
     cout<<"a="<<a<<endl;
     ++a;
}

int main()
{
     staticLocalVar(); // 第一次调用, 输出a=0
     staticLocalVar(); // 第二次调用, 记忆了第一次退出时的值, 输出a=1
     return 0;
}

二、外部静态变量/函数

在C中static有了第二种含义:用来表示不能被其它文件访问的全局变量和函数。,但为了限制全局变量/函数的作用域, 函数或变量前加static使得函数成为静态函数。但此处“static”的含义不是指存储方式,而是指对函数的作用域仅局限于本文件(所以又称内部函数)。注意此时, 对于外部(全局)变量, 不论是否有static限制, 它的存储区域都是在静态存储区, 生存期都是全局的. 此时的static只是起作用域限制作用, 限定作用域在本模块(文件)内部.
使用内部函数的好处是:不同的人编写不同的函数时,不用担心自己定义的函数,是否会与其它文件中的函数同名。

//file1.cpp

static int varA;
int varB;
extern void funA()
{
……
}

static void funB()
{
……
}

//file2.cpp

extern int varB; // 使用file1.cpp中定义的全局变量
extern int varA; // 错误! varA是static类型, 无法在其他文件中使用
extern void funA(); // 使用file1.cpp中定义的函数
extern void funB(); // 错误! 无法使用file1.cpp文件中static函数

三、静态数据成员/成员函数(C++特有)

C++重用了这个关键字,并赋予它与前面不同的第三种含义:表示属于一个类而不是属于此类的任何特定对象的变量和函数. 这是与普通成员函数的最大区别, 也是其应用所在, 比如在对某一个类的对象进行计数时, 计数生成多少个类的实例, 就可以用到静态数据成员. 在这里面, static既不是限定作用域的, 也不是扩展生存期的作用, 而是指示变量/函数在此类中的唯一性. 这也是”属于一个类而不是属于此类的任何特定对象的变量和函数”的含义. 因为它是对整个类来说是唯一的, 因此不可能属于某一个实例对象的. (针对静态数据成员而言, 成员函数不管是否是static, 在内存中只有一个副本, 普通成员函数在被调用时, 需要传入this指针, static成员函数在被调用时, 没有this指针. )

#include <iostream>
using namespace std;

class Student{
public:
    Student(char *name, int age, float score);
    void show();
public:  //声明静态成员函数
    static int getTotal();
    static float getPoints();
private:
    static int m_total;  //总人数
    static float m_points;  //总成绩
private:
    char *m_name;
    int m_age;
    float m_score;
};

int Student::m_total = 0;
float Student::m_points = 0.0;

Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score){
    m_total++;
    m_points += score;
}
void Student::show(){
    cout<<m_name<<"的年龄是"<<m_age<<",成绩是"<<m_score<<endl;
}
//定义静态成员函数
int Student::getTotal(){
    return m_total;
}
float Student::getPoints(){
    return m_points;
}

int main(){
    (new Student("小明", 15, 90.6)) -> show();
    (new Student("李磊", 16, 80.5)) -> show();
    (new Student("张华", 16, 99.0)) -> show();
    (new Student("王康", 14, 60.8)) -> show();

    int total = Student::getTotal();
    float points = Student::getPoints();
    cout<<"当前共有"<<total<<"名学生,总成绩是"<<points<<",平均分是"<<points/total<<endl;

    return 0;
}

使用class_name::static_func_name
class_name::static_var

四、为什么static变量只能被初始化一次?

所有的对象(不止是)都只会被初始化一次,但是静态变量的的生存周期比较长(放在静态存储区),普通变量是放在堆上的(生存周期短);

五、为什么static变量不要被定于在头文件中?

test_static.h

#ifndef test_static_h
#define test_static_h
#include <iostream>
#include <string>
using namespace std;

static string static_str = "staic_str";
// string static_str = "staic_str"; // 会报错,重定义

#endif /* test_static_h */

test_static1.cpp

#include "test_static.h"

void test_static1() {
    static_str = "test_static1";
    cout<<"test_static1() ==> static_test = "<<static_str<<static_str<<"  address = "<<&static_str<<endl;
}

test_static2.cpp

#include "test_static.h"

void test_static2() {
    static_str = "test_static2";
    cout<<"test_static2() ==> static_test = "<<static_str<<static_str<<"  address = "<<&static_str<<endl;
}

test_static_main.hpp和test_static_main.cpp

#ifndef test_static_main_hpp
#define test_static_main_hpp

#include <iostream>
#include "test_static.h"

void test_static1();
void test_static2();

#endif /* test_static_main_hpp */


#include "test_static_main.hpp"

int main()
{
    test_static1();
    test_static2();
    cout<<"static_str = "<<static_str<<static_str<<"  address = "<<&static_str<<endl;
    return 0;
}

运行结果

test_static1() ==> static_test = test_static1  address = 0x100010288
test_static2() ==> static_test = test_static2  address = 0x100010258
static_str = static_str  address = 0x100010270

当头文件中定义static变量的时候,该static变量并不能被多个文件共享,static变量具有内连接属性,在其他函数中被使用的时候,会产生一个临时变量,此时的static变量是不能作为全局变量的;如果需要作为全局变量被所有的文件共享,可以使用extern关键字。修改代码如下:
修改test_static.h

#ifndef test_static_h
#define test_static_h
#include <iostream>
#include <string>
using namespace std;

// string str = "staic_str"; // 会报错,重定义
// static string str = "static_str";
extern string str;

#endif /* test_static_h */

修改test_static_main.hpp

#ifndef test_static_main_hpp
#define test_static_main_hpp

#include <iostream>
#include "test_static.h"

string str = "extern str";
void test_static1();
void test_static2();

#endif /* test_static_main_hpp */

运行结果:

test_static1() ==> static_test = test_static1  address = 0x100010248
test_static2() ==> static_test = test_static2  address = 0x100010248
str = test_static2  address = 0x100010248

extern变量不能在声明的时候被初始化,同时不能在声明的文件被初始化,否则会出现多次初始化的错误。
因此我们在test_static_main_hpp完成对extern变量str的初始化。

上一篇 下一篇

猜你喜欢

热点阅读