- 論壇徽章:
- 1
|
原帖由 converse 于 2005-11-24 23:49 發(fā)表
可以在頭文件中類體內(nèi)初始化有序型的const靜態(tài)常量,但是還是需要在文本文件中定義一下這個(gè)類,但是不能再初始化了:
- //c.h
- class test
- {
- static const int a = 1; //在類體內(nèi)初始化
- };
- // c ...
- const int test::a; // 必須的成員定義
復(fù)制代碼
不知道到底是不是必需的,如:
- #include <iostream>
- using namespace std;
- class T{
- public:
- static const int count = 100;
- };
- int main()
- {
- cout << T::count << endl;
- }
復(fù)制代碼[test:/home/test/src/temp]g++ test.cpp
[test:/home/test/src/temp]./a.out
100 - cout << T::count << endl;//改成
- cout << static<void *>(&T::count) << endl;
復(fù)制代碼/home/test/src/temp/test.cpp:12: undefined reference to `T::count' 所以好像只在需要它真實(shí)存儲(chǔ)的時(shí)候才需要它的具體的定義.不過書上都說(shuō)要定義的.包括TCPL吧.
[ 本帖最后由 THEBEST 于 2005-11-25 09:06 編輯 ] |
|