標題:
想請教一下c 的this的用法
發問:
this->width=516; 這個我就不懂了= = 因為工具書上解釋太少了Q口Q 更新: 有大大可以給我C 的THIS我剛剛打錯了對不起塞麟娘
最佳解答:
this 是c++的保留字。 所謂的this,講白話一點,就是"自己" 。 舉個例子, class Rect { public: int getWidth() { return this->width; } void setWidth( int widthValue) { this->width = widthValue ;} private: int width; int legth; }; Rect myRect; myRect.setWidth(516); int myWidth = myRect.getWidth(); 結果是 myWidth 也會是516 也就是說,this是個指標,指向物件本身。
其他解答:
this結構 指向的width 要注意 "." 與 "->" 的差別喔 一個是結構 一個是結構指標 EX typedef struct { int height; int width; }XXX; //以上 定義一個結構 內容包含兩個int成員 XXX abc;//宣告一個XXX結構 結構名稱abc abc.height=123;//塞值進去此abc內的兩個成員 abc.width=456; printf(" . height %d
",abc.height); printf(" . width %d
",abc.width); XXX *ptr=&abc; //宣告一個可以指向XXX結構的指標 名稱為ptr 並將此指向abc結構 //注意一下 . 與 ->的差異 printf(" -> height %d
",ptr->height); printf(" -> width %d
",ptr->width); printf(" -> height %d
",(*ptr).height); printf(" -> width %d
",(*ptr).width); 2006-12-15 11:38:53 補充: = = 我的答案可能不是你要的 this是保留字 剛剛想到 我很少用到 我用C 比較沒有OOP觀念 this 似乎跟繼承有關 期待其他高手回答 ~~~~~ 2006-12-15 13:24:11 補充: 我印象中 this->width= 是顯性的表示法 其實只要直接打上width= (隱性表示法) 也是可以動作的 EX 以下是我在書本上看到的範例 C int A//全欲變數 void main() { A=123//隱性表示法 this.A=123;//顯性表示法 } @@應該有錯
- 我想問港元500蚊no. JP138888值幾錢呢-
- 如何用其他帳號申請MSN帳號
- 單位換算我不會-急!!20點!!
- 好像是數學連比的問題...
- flumpool 山村隆太一問
- 從〝北縣林口高中→→北市四平街〞 公車怎坐--
- UTEC 503手機該修嗎?
- amd 2500+的cpu適用於目前市面上哪些主機板-
- 必翔電動代步車與輪椅車的售價?
- 台中火車站到家樂福文心店..急~~~~
此文章來自奇摩知識+如有不便請留言告知
6FE6251C5F3F8A0F