雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

C++中this指針的理解與作用詳解

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊:

01 C++ 程序到 C 程序的翻譯

要想理解 C++ 的 this 指針,我們先把下面的 C++ 代碼轉換成 C 的代碼

class Car 
{
public:
 int m_price;   // 成員變量
 void SetPrice(int p) // 成員函數(shù)
 {
  m_price = p; 
 }
};

int main()
{
 Car car;
 car.SetPrice(20000); // 給car對象m_price成員變量賦值
 
 return 0;
}

C 語言是沒有類定義的class關鍵詞,但是有跟class類似的定義,那就是結構體struct。

m_price變量是Car類的成員變量,那么我們可以把Car類和成員變量翻譯成如下的 C 代碼:

// 結構體Car
struct Car
{
 // price變量是屬于Car結構體這個域里的變量
 int price; 
};

SetPrice函數(shù)是Car類的成員函數(shù),但是 C 程序里是沒有成員函數(shù)這種概念的,所以只能把成員函數(shù)翻譯成全局的函數(shù):

// 參數(shù)1:結構體Car的指針
// 參數(shù)2:要設置的價格變量
void SetPrice(struct Car* this, int p)
{ 
 this->price = p; // 將傳入的Car結構體的price變量賦值
}

為什么要加個 this 的指針呢?我們繼續(xù)往下看。

在這里我們把上面main函數(shù)下面的 C++ 程序翻譯 C 程序是這樣的:

int main() 
{
 struct Car car;
 SetPrice( &car, 20000);
 return 0;
}

所以最終把上述的 C++程序 轉換成C 程序的代碼如下:

struct Car
{
 int price; 
};


void SetPrice(struct Car* this, int p)
{ 
 this->price = p; 
}

int main() 
{
 struct Car car;
 SetPrice( &car, 20000); // 給car結構體的price變量賦值
 return 0;
}

02 this指針的作用

其作用就是指向成員函數(shù)所作用的對象,

所以非靜態(tài)成員函數(shù)中可以直接使用 this 來代表指向該函數(shù)作用的對象的指針。

#include <iostream>

class Car 
{
public:
 int m_price;
 
 void PrintPrice()
 {
  std::cout << m_price << std::endl; 
 }
 
 void SetPrice(int p)
 {
  this->m_price = p; // 等價于 m_price = p;
  this->PrintPrice();// 等價于 PrintPrice();
 }
 
 Car GetCar()
 {
  return *this; // 返回該函數(shù)作用的對象
 }
};

int main(void)
{
 Car car1, car2;
 car1.SetPrice(20000);
 
 // GetCar()成員函數(shù)返回所作用的car1對象,所把返回的car1賦值給了car2
 car2 = car1.GetCar(); 
 car2.PrintPrice(); 
 
 return 0;
}

輸出結果:

20000
20000

接下來我們下面的代碼,你覺得輸出結果是什么呢?會出錯嗎?

class A
{
 int i;
 public:
 void Hello() { cout << "hello" << endl; }
};

int main()
{
 A * p = NULL;
 p->Hello(); //結果會怎樣?
}

答案是正常輸出hello,你可能會好奇明明 p 指針是空的,不應該是會程序奔潰嗎?別著急,我們先把上面的代碼轉換C程序,就能理解為什么能正常運行了。

void Hello() { cout << "hello" << endl; } 
# 成員函數(shù)相當于如下形式:
void Hello(A * this ) { cout << "hello" << endl; }

p->Hello(); 
# 執(zhí)行Hello()形式相當于:
Hello(p);

所以,實際上每個成員函數(shù)的第一個參數(shù)默認都有個指向對象的 this 指針,上述情況下如果該指向的對象是空,相當于成員函數(shù)的第一個參數(shù)是NULL,那么只要成員函數(shù)沒有使用到成員變量,也是可以正常執(zhí)行。

下面這份代碼執(zhí)行時,就會奔潰了,因為this指針是空的,使用了 空的指針指向了成員變量i,程序就會奔潰。

class A
{
  int i;
public:
  void Hello() { cout << i << "hello" << endl; }
  // ->> void Hello(A * this ) { cout << this->i << "hello" << endl; }
};
int main()
{
  A * p = NULL;
  p->Hello(); // ->> Hello(p); 
}

03 this指針和靜態(tài)成員函數(shù)

靜態(tài)成員函數(shù)是不能使用 this 指針,因為靜態(tài)成員函數(shù)相當于是共享的變量,不屬于某個對象的變量。

04 小結

通過將C++程序翻譯成C程序的方式,來理解 this 指針,其作用就是指向非靜態(tài)成員函數(shù)所作用的對象,每個成員函數(shù)的第一個參數(shù)實際上都是有個默認 this 指針參數(shù)。

靜態(tài)成員函數(shù)是無法使用this指針,

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對我們的支持。

上一篇:C語言實現(xiàn)餐飲結賬管理系統(tǒng)

欄    目:C語言

下一篇:餐館點菜系統(tǒng)C語言源代碼

本文標題:C++中this指針的理解與作用詳解

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/118.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有