[問題] static & ::
看板C_and_CPP (C/C++)作者ot32em (reverse(wasefo);)時間16年前 (2009/11/21 21:13)推噓1(1推 0噓 6→)留言7則, 3人參與討論串1/1
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 )
( 未必需要依照此格式,文章條理清楚即可 )
遇到的問題: (題意請描述清楚)
1. :: 的功能除了 取 static, 宣告 member function,
為什麼也可以在member裡取用object的member(非class的member)
ex: class point{public: int x;
int getX();}
int point::getX(){ return point::x; };
2.1 還有啊就是參數名跟member名一樣時 ex void setX(int x){ x = x; }
為什麼編譯器會知道是 this->x = x;
而不是 x = this->x or x = x;
2.2 我用 cout 出 x 跟 this->x 的值也不一樣 x 就是參數 this->x 就是member x
他們的優先權是如果定義的???
3. 我用 point 實作 自己定義的 printable class 裡的 const char* toString();
本來是想在 printable class
定義一個 char a[15] 來放 ( %x , %y ) 文字
後來發現這樣每個class size高達36
因為我只是一個空間來放文字的 address的
所以想說用 static
改之前
printable class 裡
const char* printBuffer[15];
point 裡的 toString();
const char* Point::toString()
{
sprintf(this->printBuffer, "( %d, %d)", this->x, this->y);
return this->printBuffer;
}
這樣 OK
可是我把 printable class 裡的printBuffer 加個static
static const char* printBuffer[15];
const char* Point::toString()
{
sprintf(Point::printBuffer, "( %d, %d)", this->x, this->y);
return Point::printBuffer;
}
這樣GG
dev-c++ debugger說 undefined reference to Point::printBuffer
4.
承 3跟2 如果我要在 member function 裡
存取自己class裡的member時
使用 ClassName::Member 不就會混掉!?
希望得到的正確結果:
可以跑 沒有error
程式跑出來的錯誤結果:
debugger says: [Linker Error] undefined refenence to Point::printBuffer
開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)
dev-c++ on windows xp
有問題的code: (請善用置底文標色功能)
the above
補充說明:
懇請幫忙了!!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.139.132.251
→
11/22 01:22, , 1F
11/22 01:22, 1F
推
11/22 12:55, , 2F
11/22 12:55, 2F
→
11/22 12:56, , 3F
11/22 12:56, 3F
→
11/22 12:57, , 4F
11/22 12:57, 4F
→
11/22 12:57, , 5F
11/22 12:57, 5F
→
11/22 12:58, , 6F
11/22 12:58, 6F
→
11/22 17:30, , 7F
11/22 17:30, 7F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章