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

static 的性質:
- 局部特性:作用范圍僅限于本函數(shù)
- 靜態(tài)特性:存儲在靜態(tài)區(qū), 函數(shù)調用結束后不孝順而保留原值. 在下一次調用時, 保留上一次調用結束時的值.
靜態(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;
// 無參構造
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;
}
輸出結果:
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;
// 無參構造
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;
}
輸出結果:
0
1
0
靜態(tài)數(shù)據(jù)成員既可以通過對象名引用, 也可以通過類名來引用. 在作用域內, 通過類名和運算符 “::” 引用靜態(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)成員的本質特征是類中所有對象的 “公共元素”
- 靜態(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;
// 無參構造
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)
};
// 調試輸出平均分
cout << "三個學生的平均分是: " << Student::average() << endl;
return 0;
}
輸出結果:
三個學生的平均分是: 78
到此這篇關于C++中static修飾符的詳解及其作用介紹的文章就介紹到這了,更多相關C++ static內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
c++創(chuàng)建二維動態(tài)數(shù)組與內存釋放問題
這篇文章主要介紹了c++創(chuàng)建二維動態(tài)數(shù)組與內存釋放問題,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06
c++實現(xiàn)跳躍表(Skip List)的方法示例
跳表(skiplist)是一個非常優(yōu)秀的數(shù)據(jù)結構,實現(xiàn)簡單,插入、刪除、查找的復雜度均為O(logN),下面這篇文章主要介紹了c++實現(xiàn)跳躍表(Skip List)的相關資料,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。2017-09-09

