溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++語言中this和static關(guān)鍵字如何使用

發(fā)布時間:2021-07-23 16:01:20 來源:億速云 閱讀:107 作者:Leah 欄目:編程語言

C++語言中this和static關(guān)鍵字如何使用,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

一.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)成員變量時不需要加staticStudent::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++語言中this和static關(guān)鍵字如何使用問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI