線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:999
推到 Plurk!
推到 Facebook!

使用this point to enable cascaded function calls

尚未結案
雲中鵝
一般會員


發表:14
回覆:24
積分:7
註冊:2004-12-21

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-04-08 13:09:35 IP:140.125.xxx.xxx 未訂閱
請問... 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 
using std::cout;
#include
using std::setfill;
using std::setw;
#include "file1.h"
#pragma argsused
time::time(int hr,int min,int sec)
{
settime(hr,min,sec);
}
time &time::settime(int h,int m,int s)
{
sethour(h);
setminute(m);
setsecond(s);
return *this;
}
time &time::sethour(int h)
{
hour=(h>=0 && h<24)?h:0;
return *this;
}    time &time::setminute(int m)
{
minute=(m>=0 && m< 60)?m:0;
return *this;
}
time &time::setsecond(int s)
{
second=(s>=0 && s<60)?s:0;
return *this;
}
int time::gethour() const 
{return houre;}
int time::getminute() const
{return minute;}    int time::getsecond() const
{return second;}    void time::printuniversal() const
{
cout<    point_this.cpp      
#include
using std::cout; //must place there ,because ??
using std::endl;
#include "file1.h"
int main()//<---error for too many type declaration
{
        time t;
        t.sethour(18).setminute(30).setsecond(11);
        cout<<"XX";
        t.printuniversal();            cout<<"YY";
        t.printstandard();
        t.settime(20,20,20).printstandard();            cout<    Try it!
        
------
Try it!
雲中鵝
一般會員


發表:14
回覆:24
積分:7
註冊:2004-12-21

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-04-08 22:17:13 IP:140.125.xxx.xxx 未訂閱
對不請大大....我找到問題了. 1.打錯字
------
Try it!
Fm
初階會員


發表:19
回覆:66
積分:37
註冊:2003-10-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-04-09 01:08:22 IP:61.62.xxx.xxx 未訂閱
雲中鵝 你好:
舉...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

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-04-10 11:13:07 IP:140.125.xxx.xxx 未訂閱
您好... 以下是我修改的程式. 但是在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:
....
};
#endif
testfile1.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

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-04-10 12:31:05 IP:61.62.xxx.xxx 未訂閱
雲中鵝....我的天ㄚ < 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

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-04-10 13:00:44 IP:140.125.xxx.xxx 未訂閱
噢...謝謝大大的解答.我只是想試試而已.. 程式只是課本的範例.我只是想改改看看而已. thx
------
Try it!
系統時間:2024-05-19 15:36:26
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!