C++中static修飾符的詳解及其作用介紹
概述
static (靜態(tài)) 修飾符是用來(lái)控制變量的存儲(chǔ)方式和可見(jiàn)性的. 靜態(tài)局部變量存儲(chǔ)在靜態(tài)區(qū)域:

static 的性質(zhì):
- 局部特性:作用范圍僅限于本函數(shù)
- 靜態(tài)特性:存儲(chǔ)在靜態(tài)區(qū), 函數(shù)調(diào)用結(jié)束后不孝順而保留原值. 在下一次調(diào)用時(shí), 保留上一次調(diào)用結(jié)束時(shí)的值.
靜態(tài)數(shù)據(jù)成員
在我們定義全局變量的時(shí)候, 我們會(huì)發(fā)現(xiàn)一個(gè)問(wèn)題:
我們可以在程序各處自由的修改全局變量的值 (不安全).
靜態(tài)數(shù)據(jù)成員的特點(diǎn):
- 靜態(tài)數(shù)據(jù)成員被所有對(duì)象共享, 在所有對(duì)象之外單獨(dú)開(kāi)辟空間存儲(chǔ)
- 靜態(tài)數(shù)據(jù)成員所占空間并不隨某個(gè)對(duì)象的撤銷(xiāo)而釋放
- 靜態(tài)數(shù)據(jù)成員可以在同類(lèi)的多個(gè)對(duì)象之間實(shí)現(xiàn)數(shù)據(jù)共享

引用靜態(tài)數(shù)據(jù)成員
Student 類(lèi):
#ifndef PROJECT1_STUDENT_H
#define PROJECT1_STUDENT_H
#include <string>
using namespace std;
class Student {
private:
static int count; // 定義靜態(tài)數(shù)據(jù)成員
int num;
string name;
char gender;
public:
Student();
Student(int num, string name, char gender);
~Student();
int getCount() {return count;}
void display();
};
#endif //PROJECT1_STUDENT_H
Student.cpp:
#include "Student.h"
#include <iostream>
using namespace std;
// 類(lèi)外初始化靜態(tài)數(shù)據(jù)成員
int Student::count = 0;
// 無(wú)參構(gòu)造
Student::Student() : num(-1), name("None"), gender('N') {}
Student::Student(int n, string p, char g) : num(n), name(p), gender(g) {
count ++;
}
void Student::display() {
cout << "num: " << num << endl;
cout << "name: " << name << endl;
cout << "gender: " << gender << endl;
cout << "===============" << endl;
}
Student::~Student() {
count --;
}
main:
#include "Student.h"
#include <iostream>
using namespace std;
int main() {
Student student1(1, "Little white", 'f');
cout << student1.getCount() << endl;
Student *pt = new Student(1, "Little white", 'f');
cout << pt -> getCount() << endl;
cout << student1.getCount() << endl;
// 釋放
delete pt;
cout << student1.getCount() << endl;
return 0;
}
輸出結(jié)果:
1
2
2
1
靜態(tài)數(shù)據(jù)成員是 “大家” 的:
- 靜態(tài)數(shù)據(jù)成員不屬于某對(duì)象, 而是屬于類(lèi)的所有對(duì)象. 不過(guò), 用類(lèi)的對(duì)象可以引用它
- 如果靜態(tài)數(shù)據(jù)成員被定義為私有的, 則不能在類(lèi)外直接引用, 而必須通過(guò)公用的成員函數(shù)引用
- 靜態(tài)數(shù)據(jù)成員實(shí)現(xiàn)了各對(duì)象之間的數(shù)據(jù)共享, 同時(shí)避免了使用全局變量破壞了封裝的原則
用類(lèi)名訪問(wèn)數(shù)據(jù)成員
Student 類(lèi):
#ifndef PROJECT1_STUDENT_H
#define PROJECT1_STUDENT_H
#include <string>
using namespace std;
class Student {
private:
int num;
string name;
char gender;
public:
static int count; // 定義靜態(tài)數(shù)據(jù)成員
Student();
Student(int num, string name, char gender);
~Student();
int getCount() {return count;}
void display();
};
#endif //PROJECT1_STUDENT_H
Student.cpp:
#include "Student.h"
#include <iostream>
using namespace std;
// 類(lèi)外初始化靜態(tài)數(shù)據(jù)成員
int Student::count = 0;
// 無(wú)參構(gòu)造
Student::Student() : num(-1), name("None"), gender('N') {}
Student::Student(int n, string p, char g) : num(n), name(p), gender(g) {
count ++;
}
void Student::display() {
cout << "num: " << num << endl;
cout << "name: " << name << endl;
cout << "gender: " << gender << endl;
cout << "===============" << endl;
}
Student::~Student() {
count --;
}
main:
int main() {
cout << Student::count << endl;
Student *pt = new Student(1, "Little white", 'f');
cout << pt -> getCount() << endl;
// 釋放
delete pt;
cout << Student::count << endl;
return 0;
}
輸出結(jié)果:
0
1
0
靜態(tài)數(shù)據(jù)成員既可以通過(guò)對(duì)象名引用, 也可以通過(guò)類(lèi)名來(lái)引用. 在作用域內(nèi), 通過(guò)類(lèi)名和運(yùn)算符 “::” 引用靜態(tài)數(shù)據(jù)成員時(shí), 不用考慮該類(lèi)知否有對(duì)象存在.
靜態(tài)成員函數(shù)
成員函數(shù)也可以定義為靜態(tài)的, 我們只需要在類(lèi)聲明函數(shù)的前面加上 static 關(guān)鍵字. 如:
#ifndef PROJECT1_STUDENT_H
#define PROJECT1_STUDENT_H
#include <string>
using namespace std;
class Student {
private:
int num;
string name;
char gender;
public:
static int count; // 定義靜態(tài)數(shù)據(jù)成員
Student();
Student(int num, string name, char gender);
~Student();
static int getCount() {return count;} // 定義靜態(tài)成員函數(shù)
void display();
};
#endif //PROJECT1_STUDENT_H
靜態(tài)成員函數(shù)的作用就是為了能處理靜態(tài)數(shù)據(jù)成員, 即不需要 this 指針訪問(wèn)的成員.
重點(diǎn):
- 靜態(tài)成員的本質(zhì)特征是類(lèi)中所有對(duì)象的 “公共元素”
- 靜態(tài)成員的語(yǔ)法特征是通過(guò)類(lèi)名和域運(yùn)算符 “::” 引用, 而不只是通過(guò)對(duì)象引用
綜合案例
Student 類(lèi):
#ifndef PROJECT1_STUDENT_H
#define PROJECT1_STUDENT_H
#include <string>
using namespace std;
class Student {
private:
int num;
string name;
char gender;
int score;
public:
static int count; // 定義靜態(tài)數(shù)據(jù)成員
static int sum; // 定義靜態(tài)數(shù)據(jù)成員
Student();
Student(int num, string name, char gender, int score);
~Student();
static double average() {return (sum / count);}
static int getCount() {return count;}
void display();
};
#endif //PROJECT1_STUDENT_H
Student.cpp:
#include "Student.h"
#include <iostream>
using namespace std;
// 類(lèi)外初始化靜態(tài)數(shù)據(jù)成員
int Student::count = 0;
int Student::sum = 0;
// 無(wú)參構(gòu)造
Student::Student() : num(-1), name("None"), gender('N'), score(-1) {}
Student::Student(int n, string p, char g, int s) : num(n), name(p), gender(g), score(s) {
count ++;
sum += s;
}
void Student::display() {
cout << "num: " << num << endl;
cout << "name: " << name << endl;
cout << "gender: " << gender << endl;
cout << "===============" << endl;
}
Student::~Student() {
count --;
}
main:
#include "Student.h"
#include <iostream>
using namespace std;
int main() {
// 創(chuàng)建student數(shù)組
Student student_array[3] = {
Student(1, "Little white", 'f', 68),
Student(2, "Small white", 'f', 78),
Student(3, "Big white", 'f', 88)
};
// 調(diào)試輸出平均分
cout << "三個(gè)學(xué)生的平均分是: " << Student::average() << endl;
return 0;
}
輸出結(jié)果:
三個(gè)學(xué)生的平均分是: 78
到此這篇關(guān)于C++中static修飾符的詳解及其作用介紹的文章就介紹到這了,更多相關(guān)C++ static內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c++創(chuàng)建二維動(dòng)態(tài)數(shù)組與內(nèi)存釋放問(wèn)題
這篇文章主要介紹了c++創(chuàng)建二維動(dòng)態(tài)數(shù)組與內(nèi)存釋放問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
基于OpenCV?差分法實(shí)現(xiàn)綠葉識(shí)別
物體識(shí)別是圖像處理學(xué)在現(xiàn)實(shí)生活中較多的應(yīng)用之一,本文提供了一種相對(duì)簡(jiǎn)單的思路來(lái)實(shí)現(xiàn)綠葉識(shí)別,適合初學(xué)圖像處理的新人研究參考。感興趣的同學(xué)可以關(guān)注一下2021-11-11
c++中string類(lèi)型和int類(lèi)型相互轉(zhuǎn)換的幾種常用方法
我們?cè)诰帉?xiě)程序時(shí),經(jīng)常涉及到int與string之間的類(lèi)型轉(zhuǎn)換,本文主要介紹了c++中string類(lèi)型和int類(lèi)型相互轉(zhuǎn)換的幾種常用方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
c++實(shí)現(xiàn)跳躍表(Skip List)的方法示例
跳表(skiplist)是一個(gè)非常優(yōu)秀的數(shù)據(jù)結(jié)構(gòu),實(shí)現(xiàn)簡(jiǎn)單,插入、刪除、查找的復(fù)雜度均為O(logN),下面這篇文章主要介紹了c++實(shí)現(xiàn)跳躍表(Skip List)的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)深入探索順序表
順序表,全名順序存儲(chǔ)結(jié)構(gòu),是線性表的一種,線性表用于存儲(chǔ)邏輯關(guān)系為“一對(duì)一”的數(shù)據(jù),順序表自然也不例外,不僅如此,順序表對(duì)數(shù)據(jù)的物理存儲(chǔ)結(jié)構(gòu)也有要求,跟隨下文來(lái)具體了解吧2022-03-03
C/C++ 實(shí)現(xiàn)簡(jiǎn)易HTTP服務(wù)器的示例
這篇文章主要介紹了C/C++ 實(shí)現(xiàn)簡(jiǎn)易HTTP服務(wù)器的示例,幫助大家更好的理解和學(xué)習(xí)C/C++編程,感興趣的朋友可以了解下2020-10-10
C語(yǔ)言結(jié)構(gòu)及隊(duì)列實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了C語(yǔ)言實(shí)現(xiàn)隊(duì)列示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12

