C++中static修飾符的詳解及其作用介紹
概述
static (靜態(tài)) 修飾符是用來控制變量的存儲方式和可見性的. 靜態(tài)局部變量存儲在靜態(tài)區(qū)域:
static 的性質:
- 局部特性:作用范圍僅限于本函數
- 靜態(tài)特性:存儲在靜態(tài)區(qū), 函數調用結束后不孝順而保留原值. 在下一次調用時, 保留上一次調用結束時的值.
靜態(tài)數據成員
在我們定義全局變量的時候, 我們會發(fā)現一個問題:
我們可以在程序各處自由的修改全局變量的值 (不安全).
靜態(tài)數據成員的特點:
- 靜態(tài)數據成員被所有對象共享, 在所有對象之外單獨開辟空間存儲
- 靜態(tài)數據成員所占空間并不隨某個對象的撤銷而釋放
- 靜態(tài)數據成員可以在同類的多個對象之間實現數據共享
引用靜態(tài)數據成員
Student 類:
#ifndef PROJECT1_STUDENT_H #define PROJECT1_STUDENT_H #include <string> using namespace std; class Student { private: static int count; // 定義靜態(tài)數據成員 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)數據成員 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)數據成員是 “大家” 的:
- 靜態(tài)數據成員不屬于某對象, 而是屬于類的所有對象. 不過, 用類的對象可以引用它
- 如果靜態(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; public: static int count; // 定義靜態(tài)數據成員 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)數據成員 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)數據成員既可以通過對象名引用, 也可以通過類名來引用. 在作用域內, 通過類名和運算符 “::” 引用靜態(tài)數據成員時, 不用考慮該類知否有對象存在.
靜態(tài)成員函數
成員函數也可以定義為靜態(tài)的, 我們只需要在類聲明函數的前面加上 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)數據成員 Student(); Student(int num, string name, char gender); ~Student(); static int getCount() {return count;} // 定義靜態(tài)成員函數 void display(); }; #endif //PROJECT1_STUDENT_H
靜態(tài)成員函數的作用就是為了能處理靜態(tài)數據成員, 即不需要 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)數據成員 static int sum; // 定義靜態(tài)數據成員 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)數據成員 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數組 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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在Visual Studio使用C++開發(fā)Metro應用
這篇文章主要介紹了在Visual Studio使用C++開發(fā)Metro應用的示例,盡管只是一個Hello world,但可以體現出VS下為開發(fā)者提供的方便,需要的朋友可以參考下2015-07-07java 中ArrayList與LinkedList性能比較
這篇文章主要介紹了java 中ArrayList與LinkedList性能比較的相關資料,需要的朋友可以參考下2017-03-03