this指標 C this指標

2021-10-11 23:33:54 字數 499 閱讀 4632

this 是 c++ 中的乙個關鍵字,也是乙個 const 指標,不可以更改指向。指向當前物件,通過它可以訪問當前物件的所有成員。
#include#includeusing namespace std;

class girlfriend

void introduce()

//introduce函式在編譯器看來是這個樣子

//void introduce(girlfriend* const this)

//}; int main()

//列印結果

my name is:女朋友,my age is:18

00cff8f4

00cff8f4

在上述**執行結果中可以看出this指標指向的就是物件的位址

C this指標 3 刪除this指標

通常通常情況下,delete操作符不應該用於刪除this指標。但是,一旦使用了delete this,則需要考慮到下面的這些情況。1 delete操作符只允許與使用new分配的物件配套使用 如果物件是用new建立的,則可以使用delete this,否則會導致未知的行為。具體可參考本人之前的這篇文章...

c this 指標詳解

首先來觀察一段 class myclass int data const void tmyclass 我們知道類的成員函式在記憶體只有乙份拷貝,而類的資料成員 不考慮靜態成員 是每個物件都有自己的乙份,所以上述 中obj1和obj2呼叫data函式是同乙個函式,但它們擁有各自的資料,所以輸出結果為0...

c this指標總結

1 限定被相似的名稱隱藏的成員 public class thisname public void getthisname string name,int num 2 將物件作為引數傳遞到別的方法中 public class thisff public string shuju public voi...