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++; //錯誤的,普通成員變量是不能被常函數改變的。
         
}

本文還有許多不足,若是哪裏寫錯了,歡迎批評和指正。

相關文章
相關標籤/搜索