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

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

引用靜態(tài)數(shù)據(jù)成員
Student 類:
#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;
// 類外初始化靜態(tài)數(shù)據(jù)成員
int Student::count = 0;
// 無參構(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ù)成員不屬于某對象, 而是屬于類的所有對象. 不過, 用類的對象可以引用它
- 如果靜態(tài)數(shù)據(jù)成員被定義為私有的, 則不能在類外直接引用, 而必須通過公用的成員函數(shù)引用
- 靜態(tài)數(shù)據(jù)成員實現(xiàn)了各對象之間的數(shù)據(jù)共享, 同時避免了使用全局變量破壞了封裝的原則
用類名訪問數(shù)據(jù)成員
Student 類:
#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;
// 類外初始化靜態(tài)數(shù)據(jù)成員
int Student::count = 0;
// 無參構(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ù)成員既可以通過對象名引用, 也可以通過類名來引用. 在作用域內(nèi), 通過類名和運算符 “::” 引用靜態(tài)數(shù)據(jù)成員時, 不用考慮該類知否有對象存在.
靜態(tài)成員函數(shù)
成員函數(shù)也可以定義為靜態(tài)的, 我們只需要在類聲明函數(shù)的前面加上 static 關鍵字. 如:
#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 指針訪問的成員.
重點:
- 靜態(tài)成員的本質(zhì)特征是類中所有對象的 “公共元素”
- 靜態(tài)成員的語法特征是通過類名和域運算符 “::” 引用, 而不只是通過對象引用
綜合案例
Student 類:
#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;
// 類外初始化靜態(tài)數(shù)據(jù)成員
int Student::count = 0;
int Student::sum = 0;
// 無參構(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 << "三個學生的平均分是: " << Student::average() << endl;
return 0;
}
輸出結(jié)果:
三個學生的平均分是: 78
到此這篇關于C++中static修飾符的詳解及其作用介紹的文章就介紹到這了,更多相關C++ static內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
QT出現(xiàn)沒有MySQL驅(qū)動手動編譯詳細步驟
這篇文章主要給大家介紹了關于QT出現(xiàn)沒有MySQL驅(qū)動手動編譯詳細步驟的相關資料,文中通過圖文介紹的非常詳細,對大家學習或者使用QT具有一定的參考學習價值,需要的朋友可以參考下2023-04-04
C++結(jié)構(gòu)體struct和類class區(qū)別詳解
struct和class有什么區(qū)別?最本質(zhì)的一個區(qū)別就是默認的訪問控制:默認的繼承訪問權(quán)限,struct是public的,class是private的。2017-11-11
在Visual Studio使用C++開發(fā)Metro應用
這篇文章主要介紹了在Visual Studio使用C++開發(fā)Metro應用的示例,盡管只是一個Hello world,但可以體現(xiàn)出VS下為開發(fā)者提供的方便,需要的朋友可以參考下2015-07-07
java 中ArrayList與LinkedList性能比較
這篇文章主要介紹了java 中ArrayList與LinkedList性能比較的相關資料,需要的朋友可以參考下2017-03-03

