Re: static variable and const method in C++
看板Programming作者previewslave (蝦蝦蝦蝦 來玩圍棋吧)時間18年前 (2008/04/12 16:01)推噓1(1推 0噓 4→)留言5則, 2人參與討論串2/3 (看更多)
嗨,陳君,在下是大師。
在函式內:
void funcA ()
{
static int x = 0; // 直接在變數的後面設定即可。
…
…
}
在類別內:(C++)
class CA()
{
static int x;
…
void fA() { … }
…
};
int CA::x = 0; // 使用範疇算子 :: 來設定。別忘了要加上型態
其他:在類別的靜態函數內,用法如同在函式內。
※ 引述《sorryChen (陳揚和)》之銘言:
: 寫了那麼多年C++很汗顏 這個基本的概念沒搞懂
: 以前只用 static const variable,直接在.h class define就assign了
: 或用 static function
: 就是沒用到成員變數 不需要new instance就可以call的函數)
: 但一直沒搞懂 首先 如果定義static (not const) variable,要在哪裡initialize
: 外面或是成員函式嗎?
: 還有 const function
: (可參看 http://en.wikipedia.org/wiki/Static_variable)
: 是說 如他那樣定義 是說可以改變const variable嗎
: 而且在他C++的例子中count並非constant..只是static
: 為什麼需要const function去回傳他呢
: 希望有大師可以回答
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 211.76.58.205
推
04/12 16:17, , 1F
04/12 16:17, 1F
→
04/12 16:18, , 2F
04/12 16:18, 2F
→
04/12 21:07, , 3F
04/12 21:07, 3F
→
04/12 21:08, , 4F
04/12 21:08, 4F
→
04/12 21:09, , 5F
04/12 21:09, 5F
討論串 (同標題文章)