C++ static的使用方法及不同含義講解
在 C++ 里,static
是一個用途廣泛的關(guān)鍵字,在不同場景下有不同含義,下面為你詳細(xì)介紹:
1. 全局變量前的 static
當(dāng) static
用在全局變量前時,它會改變變量的鏈接屬性。
- 默認(rèn)全局變量:默認(rèn)的全局變量具有外部鏈接屬性,這意味著在一個文件中定義的全局變量可以在其他文件中通過
extern
關(guān)鍵字聲明后使用。 - 靜態(tài)全局變量:在全局變量前加上
static
關(guān)鍵字后,變量變?yōu)殪o態(tài)全局變量,具有內(nèi)部鏈接屬性,只能在定義它的文件中使用,其他文件無法訪問。
示例代碼:
// file1.cpp #include <iostream> // 靜態(tài)全局變量 static int staticGlobalVar = 10; // 普通全局變量 int globalVar = 20; void printStaticGlobalVar() { std::cout << "Static global variable: " << staticGlobalVar << std::endl; } // file2.cpp #include <iostream> // extern int staticGlobalVar; // 錯誤,無法在其他文件中訪問靜態(tài)全局變量 extern int globalVar; int main() { // std::cout << staticGlobalVar << std::endl; // 錯誤 std::cout << "Global variable: " << globalVar << std::endl; return 0; }
2. 局部變量前的static
當(dāng) static
用于局部變量時,它會改變變量的存儲方式和生命周期。
- 普通局部變量:普通局部變量存儲在棧上,在函數(shù)調(diào)用結(jié)束后會被銷毀。
- 靜態(tài)局部變量:靜態(tài)局部變量存儲在全局?jǐn)?shù)據(jù)區(qū),只在第一次調(diào)用函數(shù)時初始化,之后再次調(diào)用函數(shù)時,靜態(tài)局部變量會保留上一次調(diào)用結(jié)束時的值。
示例代碼:
#include <iostream> void staticLocalVariableExample() { static int staticLocalVar = 0; // 靜態(tài)局部變量 int normalLocalVar = 0; // 普通局部變量 std::cout << "Static local variable: " << staticLocalVar << std::endl; std::cout << "Normal local variable: " << normalLocalVar << std::endl; staticLocalVar++; normalLocalVar++; } int main() { staticLocalVariableExample(); staticLocalVariableExample(); return 0; }
3. 函數(shù)前的 static
當(dāng) static
用于函數(shù)前時,它會改變函數(shù)的鏈接屬性。
- 默認(rèn)全局函數(shù):默認(rèn)的全局函數(shù)具有外部鏈接屬性,可以在其他文件中通過
extern
關(guān)鍵字聲明后調(diào)用。 - 靜態(tài)函數(shù):在函數(shù)前加上
static
關(guān)鍵字后,函數(shù)變?yōu)殪o態(tài)函數(shù),具有內(nèi)部鏈接屬性,只能在定義它的文件中調(diào)用,其他文件無法訪問。
示例代碼:
// file1.cpp #include <iostream> // 靜態(tài)函數(shù) static void staticFunction() { std::cout << "This is a static function." << std::endl; } // 普通函數(shù) void normalFunction() { std::cout << "This is a normal function." << std::endl; } // file2.cpp #include <iostream> // extern void staticFunction(); // 錯誤,無法在其他文件中調(diào)用靜態(tài)函數(shù) extern void normalFunction(); int main() { // staticFunction(); // 錯誤 normalFunction(); return 0; }
4. 類的靜態(tài)成員變量
類的靜態(tài)成員變量屬于整個類,而不是類的某個對象,所有對象共享同一個靜態(tài)成員變量。
- 存儲位置:靜態(tài)成員變量存儲在全局?jǐn)?shù)據(jù)區(qū),不隨對象的創(chuàng)建和銷毀而分配和釋放內(nèi)存。
- 初始化:靜態(tài)成員變量必須在類外進(jìn)行初始化。
示例代碼:
#include <iostream> class MyClass { public: static int staticMemberVar; // 類內(nèi)聲明靜態(tài)成員變量 int normalMemberVar; MyClass(int value) : normalMemberVar(value) {} }; // 類外初始化靜態(tài)成員變量 int MyClass::staticMemberVar = 0; int main() { MyClass obj1(10); MyClass obj2(20); obj1.staticMemberVar = 30; std::cout << "obj2.staticMemberVar: " << obj2.staticMemberVar << std::endl; // 輸出 30 return 0; }
5. 類的靜態(tài)成員函數(shù)
類的靜態(tài)成員函數(shù)屬于整個類,而不是類的某個對象,可以直接通過類名調(diào)用,也可以通過對象調(diào)用。
- 無
this
指針:靜態(tài)成員函數(shù)沒有this
指針,因此不能訪問類的非靜態(tài)成員變量和非靜態(tài)成員函數(shù),只能訪問類的靜態(tài)成員變量和靜態(tài)成員函數(shù)。
示例代碼:
#include <iostream> class MyClass { public: static int staticMemberVar; int normalMemberVar; static void staticMemberFunction() { staticMemberVar = 10; // 可以訪問靜態(tài)成員變量 // normalMemberVar = 20; // 錯誤,不能訪問非靜態(tài)成員變量 std::cout << "Static member function called. staticMemberVar: " << staticMemberVar << std::endl; } }; int MyClass::staticMemberVar = 0; int main() { MyClass::staticMemberFunction(); // 通過類名調(diào)用靜態(tài)成員函數(shù) MyClass obj; obj.staticMemberFunction(); // 通過對象調(diào)用靜態(tài)成員函數(shù) return 0; }
綜上所述,static
關(guān)鍵字在不同的位置有不同的含義,主要體現(xiàn)在改變變量和函數(shù)的鏈接屬性、存儲方式、生命周期以及實現(xiàn)類的共享成員等方面。
到此這篇關(guān)于C++ static的使用方法及不同作用的文章就介紹到這了,更多相關(guān)C++ static使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實現(xiàn)學(xué)生檔案管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)學(xué)生檔案管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05Visual Studio2022+QT6創(chuàng)建桌面應(yīng)用實現(xiàn)
本文主要介紹了Visual Studio2022+QT6創(chuàng)建桌面應(yīng)用實現(xiàn),文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02c++中虛函數(shù)和純虛函數(shù)的作用與區(qū)別
這篇文章主要介紹了c++中虛函數(shù)和純虛函數(shù)的作用與區(qū)別,需要的朋友可以參考下2014-07-07C語言數(shù)據(jù)結(jié)構(gòu)實例講解單鏈表的實現(xiàn)
單鏈表是后面要學(xué)的雙鏈表以及循環(huán)鏈表的基礎(chǔ),要想繼續(xù)深入了解數(shù)據(jù)結(jié)構(gòu)以及C++,我們就要奠定好這塊基石!接下來就和我一起學(xué)習(xí)吧2022-03-03VSCode 使用 Code Runner 插件無法編譯運行文件名帶空格的文件問題
這篇文章主要介紹了VSCode 使用 Code Runner 插件無法編譯運行文件名帶空格的文件問題,本文通過圖文實例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07