使用this point to enable cascaded function calls |
尚未結案
|
雲中鵝
一般會員 發表:14 回覆:24 積分:7 註冊:2004-12-21 發送簡訊給我 |
請問...
Q1.我建立了 file1.h(definition time CLASS)及file1.cpp(time class member-function definition)和一個主程式point_this.cpp.(均於在同一資料夾中)
但是出現error.
1.too many types in declaration
2.type 'time' may not be define
3.setminute is not a member of time
Q2.
time &time::setsecond(int s)//這行宣告是什麼用法及用途?
謝謝回答(以下是相關程式) file1.h
#ifndef file #define file class time{ public: time(int = 0,int = 0,int = 0); time &settime(int,int,int); time &sethour(int); time &setminte(int); time &setsecond(int); int gethour() const; int getminute() const; int getsecond() const; void printuniversal () const; void printstandard() const; private: int hour; int minute; int second;file1.cpp #include
------
Try it! |
雲中鵝
一般會員 發表:14 回覆:24 積分:7 註冊:2004-12-21 發送簡訊給我 |
|
Fm
初階會員 發表:19 回覆:66 積分:37 註冊:2003-10-15 發送簡訊給我 |
雲中鵝 你好:
舉...sethour(...)來說 在class time裡面的成員函式,C 會附上一個this指標,用來標示是誰在調用 class time{ public: ..... time &sethour(int,(time*)this); }; .... 當你instantiation出一個time object如下 time t; 而且用t來調用其成員函式sethou(...)時,C 實做出來的碼其實是 time::sethor(int h,(time*)&t)//不一定放在最後面啦! { hour=(h>=0 && h<24)?h:0; return *this;//this也就是,(time*)&t; } //------------------- 因此設計time& time::sethor(int h) 使得該行得以成立 t.sethour(18).setminute(30).setsecond(11); 猜開來看 t.sethour(18)回傳值形態為time&,所以可以繼續呼叫setminute(30)....等等 //新手上路,請多包涵。 /*青青子矜,悠悠我心。但為君故,沈吟至今。*/ |
雲中鵝
一般會員 發表:14 回覆:24 積分:7 註冊:2004-12-21 發送簡訊給我 |
您好...
以下是我修改的程式.
但是在time &settime(int,int,int,(time*)this);出現了.expected error 謝謝回答
file1.h
#ifndef file_h #define file_h class time{ public: time(int = 0,int = 0,int = 0); time &settime(int,int,int,(time*)this); time &sethour(int,(time*)this); time &setminute(int,(time*)this); time &setsecond(int,(time*)this); ......... private: .... }; #endiftestfile1.cpp time::settime(int h,int m,int s,(time*)this)) { sethour(h,(time*)this); setminute(m,(time*)this); setsecond(s,(time*)this); return *this;/*call x.func(y) , where y is a member of X , the keyword this is set to &x and y is set to this->y, which is equivalent to x.y.Static member functions do not have a this pointer because they are called with no particular object in mind. */ } time::sethour(int h,(time*)this)) { hour=(h>=0 && h<24)?h:0;//this is when "y"==h,els ==0 return *this; }thispoint.cpp #include"testfile1.cpp" ....... t.sethour(18,t).setminute(30,t).setsecond(10,t);Try it!
------
Try it! |
Fm
初階會員 發表:19 回覆:66 積分:37 註冊:2003-10-15 發送簡訊給我 |
雲中鵝....我的天ㄚ>
< class="code">
你不是要問
"但是我仍然不懂此用法time &time::sethor(int h)"
and
使用this point to enable cascaded function calls 我的解釋讓你產生誤會真抱歉,我只是要解釋this指標在在class的地位
以及time class 如何 enable cascaded function call 我沒有要你修改原程式碼的意思 this指標是C 編譯器自動幫你加進來的你無須去增加這個augument
到你的函式去 原本的程式碼不能run嗎??不能達到你的要求嗎??
還是有其他問題,但是請先拿掉你自己增加的this指標
在po你的問題出來
//新手上路,請多包涵。 /*青青子矜,悠悠我心。但為君故,沈吟至今。*/ |
雲中鵝
一般會員 發表:14 回覆:24 積分:7 註冊:2004-12-21 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |