C++キーワードを理解する(2)---staticキーワード


他人が書いたコードを自分で作成したり読んだりするとき、よく「static」キーワードに遭遇しますが、本稿では「static」キーワードに関する知識点をまとめます.
1、staticキーワードの応用シーン
全体的に、主に以下の5つのシーンに適用されます.
プロシージャ向け:(1)静的ローカル変数の宣言(2)静的グローバル変数の宣言(3)静的関数オブジェクト向けの宣言:(4)クラスの静的メンバー変数の宣言(5)クラスの静的メンバー関数の宣言
2、staticキーワード応用詳細
本文c++メモリ管理では,コンピュータに対するC++のメモリ割り当てと使用について詳しく述べた.では、staticキーワードで修飾に成功した変数は、静的データ領域(グローバルデータ領域とも呼ばれる)に格納される.
2.1静的ローカル変数
(1)ローカル変数の前に「static」キーを付けると、静的ローカル変数になります.(2)一般的に関数内で宣言、定義され、役割ドメインは関数内部または文ブロックである.(3)デフォルトの初期値は0で、宣言と定義は一度しか実行されず、後で静的ローカル変数が存在する関数を呼び出すと、宣言と定義がスキップされます.(4)グローバルデータ領域(静的データ領域)に格納する.
C++プログラムの例、プログラムの実行結果と説明は注釈に示す
#include
using namespace std;

void staticTest()
{ 
    static int static_local_value;       //        ,      0,        
    static_local_value++;
    cout<<"           :   "<void normFunc()
{
    cout<<"          "<//    ,  static_local_value     staticTest()    
    int norm_value=0;
    norm_value++;
    cout<<"         :   "<int main()
{
    normFunc();         //     1
    normFunc();         //     1
    normFunc();         //     1

    staticTest();       //     1
    staticTest();       //     2,    staticTest()   ,   static_local_value   
    staticTest();       //     3

    return 0;
}

2.2静的グローバル変数
特徴:(1)グローバル変数の前に「static」キーを付けると静的グローバル変数になり、デフォルトの初期値は0になります.(2)役割ドメインはプログラムファイル全体にあるが、他のプログラムファイルにアクセスできない.(3)静的データ領域(グローバルデータ領域とも呼ばれる)に格納する.
C++プログラムの例、プログラムの実行結果と説明は注釈の中で与える:
"   1.cpp "

#include
using namespace std;

void file2Func();                  //   2     file2Func()
static int static_global_value;    //      ,     0
int global_value=0;                //    

void staticTest()
{
    static_global_value++;
    global_value++;
    cout<<"  1 ,         :  "<cout<<"       :  "<int main()
{
    staticTest();
    file2Func();
    return 0;
} 

"   2.cpp "
#include
using namespace std;
extern int global_value;
extern int static_global_value;   
void file2Func()
{
    cout<<"  2 ,     :   "<cout<<"       :   "<>endl;   //    ,    static_global_value
}

2.3静的関数
(1)通常関数の前にstaticキーワードを付ける.(2)静的関数の役割ドメインは本プログラムファイルに限られ、他のファイルにはアクセスできない.(3)これにより、他のファイルに静的関数と同名の関数が存在し、衝突を起こさないことができる.
C++プログラムの例:
"   1.cpp "

#include
using namespace std;
static void file2StaticFunc();    //  2.cpp      
void normFunc();                  //  2      

void static staticFunc()
{
    cout<<"    1      。"<int main()
{
    staticFunc();              //      
    normFunc();                //      
    file2StaticFunc();         //    , file2StaticFunc()   

    return 0;
}

"   2.cpp "
#include
using namespace std;

void file2StaticFunc()
{
    cout<<"    2      。"<void normFunc()
{
    cout<<"    2      。"<

2.4クラスの静的メンバー変数
(1)クラスのメンバー変数の前に「static」キーを付ける.(2)クラス内で宣言し、public、privateまたはprotectedメンバーであり、アクセス権限は通常のメンバー変数と同じである.(3)クラス外で定義,利用:=定義;(4)クラスの静的メンバー変数はクラス全体を入力し、すべてのクラスのオブジェクトに共有され、「クラス名::クラスの静的メンバー変数名」でアクセスできます.(5)静的データ領域に格納する.
2.5クラスの静的メンバー関数
(1)thisポインタがなく、クラスのすべてのオブジェクトに共有されます.(2)クラス内の静的メンバー変数と静的メンバー関数のみにアクセスでき、非静的メンバー変数と非静的メンバー関数にアクセスできない.(3)クラスの非静的メンバー関数は、静的メンバー変数と静的メンバー関数を呼び出すことができる.(4)「クラス名::関数名」でアクセスできます.
2.4と2.5の2つのケースについて、以下にc++言語で例を挙げて説明する.
" testStatic.h   "

#include
using namespace std;

class testStatic{
public:
        testStatic(int temp);       //    
        ~testStatic(){}             //    
        static int static_value;    //             
        void coutNum() 
        {
            static_value++;
            cout<<"         :  "<static void staticFunc()     //      
        {
            cout<//cout<
        }

private:
        int normValue;
};

int testStatic::static_value=5;     //        

testStatic::testStatic(int temp)
{
    normValue=temp;
}

"   main.cpp "

#include"testStatic.h"
#include
using namespace std;

int main()
{
    testStatic test1(3),test2(4),test3(5);
    test1.coutNum();   //         :6
    test2.coutNum();   //         :7
    test3.coutNum();   //         :8

    cout<//  8,  static_value public  
    cout<<:static_value class="hljs-comment">//  8,  static_value         

    test1.staticFunc();                     //  9,                  
    test2.staticFunc();                     //  10
    testStatic::staticFunc();               //  11,              
}