Delphi VJ VB VC 討論 聯繫 首頁
MS VC 參考文件

 運算子
 輸出與輸入
 流程控制
 陣列
 指標應用
 建立函數
 參數傳遞
 動態配置記憶體
 類別物件
 建構子與解建構子
 this 指標
 例外處理
 資料流
 過載運算子
 fsteram 類別

作品介紹

免費下載

廠商軟體

this指標的使用

this指標是一個指向自己的指標,可以告訴使用者,可以告訴使用者,這個物件存在於記憶的什麼地方。

//小誌的This指標範例
#include
<iostream.h>

class this_ptr //開始宣告類別
{
private: //宣告屬性
long index;
public: //宣告方法
void show_address() //輸出this指標
{ cout << "this pointer : " << this <<endl;}
};

void main() //主程式開始
{
this_ptr object;
cout << "The address of object : " << &object << endl;
//輸出物件的位址
object.show_address(); //顯示this指標指向的位址
} //主程式結束


利用this來指定存取自己的屬性與成員函數,顯然不是this指標的用途,this指標最主要的用途是傳遞物件本身。

//小誌的This指標範例
#include <iostream.h>

class this_ptr //開始宣告類別
{
private:
//宣告屬性
long index;
public: //宣告方法
void show_index() //輸出index屬性
{ cout << "index : " << index <<endl; }
this_ptr * set_index(
long i_index) //設定index屬性,並傳回一個物件的參考
{
index = i_index;

return this; //傳回物件本身
}
};

void main() //主程式開始
{
this_ptr object, * object_ptr;
object_ptr = object.set_index(1);
//設定index屬性,並設定object_ptr
cout << "object address : " << &object << endl; //輸出物件的位址
cout << "pointer : " << object_ptr << endl; //輸出指標指向的位址值
object.set_index(2)->show_index(); //設定index屬性,並顯示index屬性
} //主程式結束

 



本網頁由
昱得資訊工作室製作(C) Copyright Valor-Studio since in 1993

 

本網站全部內容係屬【昱得資訊工作室】版權所有,轉載必究,
非經正式書面同意,不得將全部或部分內容,以任何方式轉載於其他網站或用於任何商業行為