請教C++的this

看板Programming作者時間18年前 (2006/11/02 21:32), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/3 (看更多)
#include <iostream> #include <cstdlib> using namespace std; class work { public: int Id; char name[20]; work() { Id = 0; strcpy(name, "ZZZ"); //指定name初值 } void output() { cout << this->Id << endl; cout << (*this).Id << endl; } }; int main(void) { work work1; work1.Id=5; //work1.name="quota123"; work1.output(); system("pause"); return 0; } 請教一下 以上程式的 this->Id 與(*this).Id 我在網路上看到陳老師的網頁 他是這樣說明: 「this」指標代表目前這個物件的指標; 「*this」則代表目前這個物件的內容 可是.為何我這裡得到的答案都是5呢? 另外我想問.. strcpy這一行程式是我看到範例原本就有的 可是 如果我想要給他資料 比如work1.name="quota123"; 卻會出現錯誤 請問..work class內的這個成員 我該如何使用呢? 謝謝 -- ┌─────KKCITY─────┐ KKBOX 可立刻 聽音樂 ■ bbs.kkcity.com.tw ■■所有想找的歌通通不必等 ■■ └──From:210.208.147.233 ──┘ http://www.kkbox.com.tw --
文章代碼(AID): #15IVDJ00 (Programming)
討論串 (同標題文章)
以下文章回應了本文
完整討論串 (本文為第 1 之 3 篇):
1
1
18年前, 11/02
文章代碼(AID): #15IVDJ00 (Programming)