C++語言基礎(chǔ) this和static關(guān)鍵字
一.this關(guān)鍵字
this是一個指針,可用其訪問成員變量或成員函數(shù)
下面是使用this的一個完整示例:
#include <iostream> using namespace std; class Student{ public: void setname(char *name); void setage(int age); void setscore(float score); void show(); private: char *name; int age; float score; }; void Student::setname(char *name){ this->name = name; } void Student::setage(int age){ this->age = age; } void Student::setscore(float score){ this->score = score; } void Student::show(){ cout<<this->name<<"的年齡是"<<this->age<<",成績是"<<this->score<<endl; } int main(){ Student *pstu = new Student; pstu -> setname("李華"); pstu -> setage(16); pstu -> setscore(96.5); pstu -> show(); return 0; }
運行結(jié)果:
李華的年齡是16,成績是96.5
this 只能用在類的內(nèi)部,通過 this 可以訪問類的所有成員,包括 private、protected、public 屬性的。
本例中成員函數(shù)的參數(shù)和成員變量重名,只能通過 this 區(qū)分。以成員函數(shù)setname(char *name)為例,它的形參是name,和成員變量name重名,如果寫作name = name;這樣的語句,就是給形參name賦值,而不是給成員變量name賦值。而寫作this -> name = name;后,=左邊的name就是成員變量,右邊的name就是形參,一目了然。
二.static 關(guān)鍵字
2.1 static 靜態(tài)成員變量
類似于java,C++中也有static靜態(tài)成員變量,用法如下:
#include <iostream> using namespace std; class Student { public: Student(char *name, int age, float score); void show(); public: static int m_total; // 靜態(tài)成員變量 private: char *m_name; int m_age; float m_score; }; int Student::m_total = 0; // 初始化靜態(tài)成員變量時不需要加static Student::Student(char *name, int age, float score) { } void Student::show() { } int main() { Student::m_total = 10; // 可以直接由類名訪問 // 棧區(qū) Student stu("Jack",15,92.5f); stu.m_total = 20; // 也可以直接由對象名訪問 // 堆區(qū) Student *pstu = new Student("Tom",16,96); pstu->m_total = 20; // 也可以直接由對象名訪問 delete pstu; return 0; }
注意:
1) 一個類中可以有一個或多個靜態(tài)成員變量,所有的對象都共享這些靜態(tài)成員變量,都可以引用它。
2) static 成員變量和普通 static 變量一樣,都在內(nèi)存分區(qū)中的全局數(shù)據(jù)區(qū)分配內(nèi)存,到程序結(jié)束時才釋放。這就意味著,static 成員變量不隨對象的創(chuàng)建而分配內(nèi)存,也不隨對象的銷毀而釋放內(nèi)存。而普通成員變量在對象創(chuàng)建時分配內(nèi)存,在對象銷毀時釋放內(nèi)存。
3) 靜態(tài)成員變量必須初始化,而且只能在類體外進行。例如:
int Student::m_total = 0; // 初始化靜態(tài)成員變量時不需要加static
初始化時可以賦初值,也可以不賦值。如果不賦值,那么會被默認初始化為 0。全局數(shù)據(jù)區(qū)的變量都有默認的初始值 0,而動態(tài)數(shù)據(jù)區(qū)(堆區(qū)、棧區(qū))變量的默認值是不確定的,一般認為是垃圾值。
4) 靜態(tài)成員變量既可以通過對象名訪問,也可以通過類名訪問,但要遵循 private、protected 和 public 關(guān)鍵字的訪問權(quán)限限制。當(dāng)通過對象名訪問時,對于不同的對象,訪問的是同一份內(nèi)存。
2.2 static 靜態(tài)成員函數(shù)
下面演示static 靜態(tài)成員函數(shù)的用法:
#include <iostream> using namespace std; class Student{ public: Student(char *name, int age, float score); void show(); public: //聲明靜態(tài)成員函數(shù) static int getTotal(); static float getPoints(); private: static int m_total; //總?cè)藬?shù) static float m_points; //總成績 private: char *m_name; int m_age; float m_score; }; int Student::m_total = 0; float Student::m_points = 0.0; Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score){ m_total++; m_points += score; } void Student::show(){ cout<<m_name<<"的年齡是"<<m_age<<",成績是"<<m_score<<endl; } //定義靜態(tài)成員函數(shù) int Student::getTotal(){ return m_total; } float Student::getPoints(){ return m_points; } int main(){ (new Student("小明", 15, 90.6)) -> show(); (new Student("李磊", 16, 80.5)) -> show(); (new Student("張華", 16, 99.0)) -> show(); (new Student("王康", 14, 60.8)) -> show(); int total = Student::getTotal(); float points = Student::getPoints(); cout<<"當(dāng)前共有"<<total<<"名學(xué)生,總成績是"<<points<<",平均分是"<<points/total<<endl; return 0; }
注意:
1) 靜態(tài)成員函數(shù)與普通成員函數(shù)的根本區(qū)別在于:普通成員函數(shù)有 this 指針,可以訪問類中的任意成員;而靜態(tài)成員函數(shù)沒有 this 指針,只能訪問靜態(tài)成員(包括靜態(tài)成員變量和靜態(tài)成員函數(shù))。
2) 上例中的getTotal()、getPoints() 也可以聲明為普通成員函數(shù),但是它們都只對靜態(tài)成員進行操作,加上 static 語義更加明確。
3) 和靜態(tài)成員變量類似,靜態(tài)成員函數(shù)在聲明時要加 static,在定義時不能加 static。靜態(tài)成員函數(shù)可以通過類來調(diào)用(一般都是這樣做),但也可以通過對象來調(diào)用。
相關(guān)文章
C++中new/delete與malloc/free的區(qū)別小結(jié)
本文主要介紹了C++中new/delete與malloc/free的區(qū)別小結(jié), malloc、free是C中的庫函數(shù) new、delete 是C++當(dāng)中的操作符,讀者可以更好地理解C++中內(nèi)存管理的方式和優(yōu)勢2023-08-08虛函數(shù)與純虛函數(shù)(C++與Java虛函數(shù)的區(qū)別)的深入分析
本篇文章是對虛函數(shù)與純虛函數(shù)進行了詳細的分析介紹,需要的朋友參考下2013-06-06C/C++自主分配出現(xiàn)double free or corruption問題解決
這篇文章主要為大家介紹了C/C++出現(xiàn)double free or corruption問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04C++使用chrono庫處理日期和時間的實現(xiàn)方法
C++11 中提供了日期和時間相關(guān)的庫 chrono,通過 chrono 庫可以很方便地處理日期和時間,本文主要介紹了C++使用chrono庫處理日期和時間的實現(xiàn)方法,感興趣的小伙伴們可以參考一下2021-09-09