• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Where there is a dream ,there is hope

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              64 Posts :: 0 Stories :: 8 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(1)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            static關鍵字在C和C++中的用法稍有區別,主要是C++擴展了static關鍵字的作用
            C 用法
            1.修飾函數成員變量:例

            //test.h
            void test_static_var();

            //test.c
            void test_static_var()
            {
                
            static int var=0;//!不初始化默認為0
                var++;
                printf(
            "static int var : %d \n", var);
            }


            //main.c
            int main()
            {
                
            for (int i=0; i<10; i++)
                
            {
                    test_static_var();
                }

                getchar();
                
            return 0;
            }

            2.在文件級別(即:在函數體外)修飾 變量或者函數,則表示該變量只在該文件中可見及可用
            //test.h
            void test_static_funtion_one();
            void test_static_fuction_two();

            //test.c
            void test_static_funtion_one()
            {
                printf(
            "This is a normal function. \n");
            }

            static
            void test_static_fuction_two()
            {
                printf(
            "This is a function modified by static. \n");
            }


            //main.c

            int main()
            {
                test_static_funtion_one();
                test_static_fuction_two();
                getchar();
                
            return 0;
            }

            這樣就會報錯:LNK2001: 無法解析的外部符號 "void __cdecl test_static_fuction_two(void)" (?test_static_fuction_two@@YAXXZ)
            原因就是test_static_fuction_two()被修飾為static ,如果main中不調用的話,編譯可以通過(VS08),否則便以失敗
            修飾為static后,它只能在test.c中使用。

            C++中包含了前兩種方法,又擴展了下面兩種方法:
            3.修飾class 數據成員,稱為 靜態數據成員
            4.修飾class 函數成員,稱之為 靜態成員函數
            //test.h
                 class TestA
                 
            {
                 
            public:
                     
                     
            static int s_count;
                     
            static int getCount();
                 
            public:
                     
            int m_value;
                 }
            ;

            //test.c
                int TestA::s_count=0;
                
            int TestA::getCount()
                
            {
                    
            //m_value+=m_value; 靜態函數中只能操作靜態數據成員
                    return s_count;
                }
            因為靜態成員函數沒有傳入隱式的this指針,所以,它不能使用. 、->操作符 ;不能是virtual 的,不能和非靜態的同名
            posted on 2011-02-16 12:21 IT菜鳥 閱讀(203) 評論(0)  編輯 收藏 引用
            久久综合五月丁香久久激情| 久久精品人妻一区二区三区| 久久精品毛片免费观看| 国产成人精品久久| 国产精品久久成人影院| 九九久久精品国产| 久久棈精品久久久久久噜噜| 国产L精品国产亚洲区久久| 中文成人无码精品久久久不卡 | 97久久精品午夜一区二区| 69SEX久久精品国产麻豆| 久久久久这里只有精品| 亚洲va久久久噜噜噜久久| 国产精品va久久久久久久| 人妻少妇久久中文字幕| 日韩va亚洲va欧美va久久| 久久精品男人影院| 久久亚洲国产成人精品性色| 国产精品无码久久四虎| 99精品久久久久中文字幕| 精品综合久久久久久98| 日韩AV毛片精品久久久| 久久国产精品免费一区二区三区| 久久亚洲春色中文字幕久久久| 日韩精品无码久久一区二区三| 91精品观看91久久久久久| 久久777国产线看观看精品| 久久综合香蕉国产蜜臀AV| 99久久免费国产精品特黄| 久久久久久极精品久久久| 久久av免费天堂小草播放| 9191精品国产免费久久| 亚洲乱亚洲乱淫久久| 青青草国产精品久久久久| 国产精品久久久久…| 97久久久精品综合88久久| 成人综合伊人五月婷久久| 婷婷五月深深久久精品| 色偷偷88888欧美精品久久久| 日产精品久久久久久久性色| 日日噜噜夜夜狠狠久久丁香五月|