[問題] C++ class this問題

作者: anoymouse (沒有暱稱)   2014-11-22 02:14:53
書上寫
class time{
private:
int hour;
public:
void set(int h){this->hour=h;}
void print(){cout<<this->hour;}
};
想請教一下this指標 上述兩行的this是隱藏參數 可是沒有傳this進來
在裡面卻有this 感覺很奇怪
還是說其實有 像void print(time *this){cout<<this->hour;} 只是也隱藏起來了?
作者: cjcat2266 (CJ Cat)   2014-11-22 02:43:00
member function都會隱晦地傳進this pointer更正,non-static member functions
作者: anoymouse (沒有暱稱)   2014-11-22 02:44:00
所以是像我最後一行這樣傳進嗎?
作者: cjcat2266 (CJ Cat)   2014-11-22 03:34:00
是的
作者: CaptainH (Cannon)   2014-11-22 11:13:00
型態是 T * const
作者: anoymouse (沒有暱稱)   2014-11-22 12:01:00
請問T是什麼?
作者: koka813 (shift)   2014-11-22 13:47:00
template吧,想表達的意思大概就該class型別的pointer
作者: LPH66 (-6.2598534e+18f)   2014-11-22 14:56:00
嗯, 以及這個 this 指標不能被改指 (它是 const pointer)
作者: anoymouse (沒有暱稱)   2014-11-22 16:13:00
好 我再看看有問題再問 謝謝各位!
作者: Killercat (殺人貓™)   2014-11-26 04:09:00
this是keyword 意思就是「自己這個實體」

Links booklink

Contact Us: admin [ a t ] ucptt.com