c++ static修饰符浅析

c++ static修饰符浅析

下面一段是引用自effective c++ 中的一句话:ios

所谓的static对象,其寿命是从构造出来到程序结束为止(如下文章再也不赘诉)。所以stack和heap-base对象都被排除。这种对象包括global对象,定义于namespace做用域内的对象,在classes内,在函数内,以及在file做用域内被声明为static的对象。c++

因此static在c++中能够存在在一下几种状况:
1.存在于全局做用域中的静态变量函数

//全局可访问,在内存中只有一份拷贝,可被不少函数修改。
#include <iostream>

static int i = 1; //做用域是整个file

void get(){
    std::cout << "in func , the i is " << i << std::endl;
}

int main(){
        std::cout << "the i is " << i << std::endl;
        get();
        return 0;
}

2.存在于函数当中的静态变量this

// 只能在这个函数中才能被调用。
// 函数调用结束后,通常局部变量都被回收了,静态变量还存在
#include <iostream>

void get(){
        static int i = 1;
        std::cout << "the i is " << i << std::endl;
        i++;
}

int main(){
        get(); // i = 1
        get(); // i = 2
        std::cout << "the i is " << i << std::endl; // 这种是错误的
        return 0;
}

3.存在于类的成员变量中的静态变量spa

//其实原理跟函数中的静态变量相似,类实例化出来的对象被销毁后,
// 可是类变量(静态成员变量)仍是存在在内存中的
#include <iostream>

class Widget{
public:
        Widget(int i){
                a = i;  
        }
        void get();
private:
        static int a;  // 声明静态变量
};

int Widget::a = 1;  //  因为是类变量不是属于专属于一个对象的,被全部对象共享
                    // 因此须要在类外定义
void Widget::get(){
        std::cout << "the a is " << a++ << std::endl;
}

int main(){

        Widget w(1);
        w.get(); // a = 1
        w.get(); // a = 2
        return 0;
}

4.存在于类中成员函数中的静态变量code

#include <iostream>

class widget{
 public:
    widget(){}
    void get();
};

void widget::get(){
    static int i = 1;
    //成员函数中的静态变量的做用域范围跟普通局部变量的做用域范围是同样的
    std::cout << "in func, the i is " << i++ << std::endl;
}

int main(int argc, char const* argv[])
{
    widget w1;
    w1.get();  // in func, the i is 1
    widget w2;
    w2.get(); // in func, the i is 2
    return 0;
}

5.存在于命令空间中的静态变量对象

#include <iostream>

namespace Widget {
    static int i = 1;  // 在该命名空间可用

    void get(){
    std::cout << "the i is " << i++ << std::endl;
    }
} // namespace Widget

int main (){

    using namespace Widget;
    get();  //the i is 1
    get(); // the i is 2
    return 0;
}

6.存在于全局做用域的静态函数内存

// 其实跟通常的函数差很少,
// 可是它将该函数的连接属性限制为内连接,
//只能在本编译单元中使用(也就是本文件),
//不能被extern等在外部文件中引用
static void get(){
    std::cout << "this is staic global func" << std::endl;
}

int main(){
    get();
    get();
    return 0;
}

7.存在于类中的静态函数作用域

#include <iostream>

class Widget{
    public:
    Widget(int i){
        a = i;
    }
    static void get(); // 声明静态函数
    
    private:
    static int a;
    int b;
};

int Widget::a = 1;
void Widget::get(){
    std::cout << b << std::endl;   //这是错误的,由于静态函数和静态变量直接可以
                                   // Widget::get()调用,不须要实例化,因此不能
                                   // 调用只能实例化才能初始化的成员变量。
    std::cout << a << std::endl; //ok
}

int main(){

    Widget w(1);
    w.get();
    return 0;
}

总结:

无论是什么静态变量,它的lifetime是从他被构造出来到程序结束为止。
static类型的变量跟其余普通的变量的不一样在于在内存中的存在形式不一样,
例如存在于函数中的局部变量,每当调用一次函数,就会产生一个局部变
量,而存在于函数中的静态变量只在该函数第一次被调用时被初始化,然
后,而后在内存只保有一份拷贝get

补充

连接属性分为三种:

1. 内连接
2. 外连接

内连接:

static修饰的函数和变量 和 const 修饰的变量(不包含extern)都是内连接,
只能在本文件中使用,即便别的文件定义了相同的变量名也没关系。

外连接:

没有用static修饰的全局变量或者函数,都是能够做为外连接
    用extern修饰的全局变量或者函数,也是做为外部连接。
    还有一个 extern const int i = 1;这也是外部连接,由于
    extern的做用会覆盖掉const使它成为外连接。

还有一类:局部变量,它的lifetime只是在函数执行期间,因此是没有连接属性的。

常成员函数是不能修改类中成员变量的,可是静态成员变量是类变量,因此能够修改

#include <iostream>

class Widget{
    public:
    Widget(int i){
        b = i;
    }
    void set() const;
    private:
    static int a;
    int b;
};

int Widget::a = 1;
void Widget::set() const{
    a++; //这是对的,由于是静态成员变量是类变量
    b++; //错误的,普通成员变量是不能被常函数改变的。
         
}

本文还有许多不足,若是哪里写错了,欢迎批评和指正。

相关文章
相关标签/搜索