Re: [語法] C#與bcb語法的轉換
※ 引述《beebeecall (bee)》之銘言:
: 標題: [語法] C#與bcb語法的轉換
: 時間: Fri May 22 11:58:35 2009
:
: 最近想練習一個 Decorator pattern的程式,
: 在網上參考了別人的作品,
: 他的語言是C#
: 內中寫到
: public virtual string Description
: {
: get { return description; }
: set { description = value; }
: }
:
: 想請問這段程式碼該怎麼改寫,
: 就我微薄的程式能力來看,
: 這似乎是在定義Description這個變數,是嗎?
: 很抱歉我基礎打不好,所以問題可能很瞎,
: 但我真的很想學,麻煩知道的人幫忙解答囉。
:
發問不是問題, 但可不可以經過思考才定標題呢?
你整個例子裡面, 和 decorator 的關係在哪?
你問的是 C# 的 property 通常在 C++ 會怎樣表達.
: --
: ※ 發信站: 批踢踢實業坊(ptt.cc)
: ◆ From: 59.126.200.190
: 推 legnaleurc:就是 getter 和 setter ,別想太多 05/22 12:12
: → beebeecall:還是不會orz 05/22 13:10
: → stero: description =value 載入value的值之後回傳description 05/22 13:14
: → stero:小弟猜是這樣 05/22 13:15
: → Cloud:就 string getDescription(){return description;} 05/22 13:47
: → Cloud:void setDescription(string s){description = s;} 05/22 13:47
: → windincloud:樓上正解~ 05/22 14:07
通常不會這樣寫吧
getDescription 這樣寫會每次 return a copy of string.
一般來說會寫
string& getDescription() {
return description;
};
吧?
另外這類 getter method 通常是 const method (當然, 還是要視乎情況)
const string& getDescription() const {
return m_description;
}
setter 通常傳入obj 的話, 會傳 const &, 省去 copy ctor 和建立一個新 obj
void setDescription(const string& s) {
m_description = s;
}
一般使用方便來說, 比較少會真的傳入 std::string,
通常傳入 char* 會比較方便:
void setDescription(const char* s) {
m_description = s; // m_description 當然是 std::string
}
Alien
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 202.155.236.82
→
05/22 15:23, , 1F
05/22 15:23, 1F
推
05/22 15:34, , 2F
05/22 15:34, 2F
→
05/22 15:35, , 3F
05/22 15:35, 3F
→
05/22 17:03, , 4F
05/22 17:03, 4F
推
05/22 18:49, , 5F
05/22 18:49, 5F
→
05/22 20:07, , 6F
05/22 20:07, 6F
→
05/22 20:08, , 7F
05/22 20:08, 7F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章