forked from SilverMaple/STLSourceCodeNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_9_1_config3.cpp
More file actions
39 lines (30 loc) · 1.12 KB
/
1_9_1_config3.cpp
File metadata and controls
39 lines (30 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// file: 1config3.cpp
// 测试在 class template中 拥有 static data members.
// test __STL_STATIC_TEMPLATE_MEMBER_BUG, defined in <stl_config.h>
// ref. C++ Primer 3/e, p.839
// vc6[o] cb4[x] gcc[o]
// cb4 does not support static data member initialization.
// 如果编译器无法处理static member of template classes(模板类静态成员)就定义。
// 即对于模板类中,模板类型不同时的静态变量不同。
#include <iostream>
using namespace std;
template <typename T>
class TestClass {
public:
static int _data;
};
// 需要加上template<>,否则编译出错,进行内存配置
template<> int TestClass<int>::_data = 1;
template<> int TestClass<char>::_data = 1;
int main() {
cout << TestClass<int>::_data << endl;
cout << TestClass<char>::_data << endl;
TestClass<int> obji1, obji2;
TestClass<char> objc1, objc2;
cout << obji1._data << " " << obji2._data << endl;
cout << objc1._data << " " << objc2._data << endl;
obji1._data = 3;
objc2._data = 4;
cout << obji1._data << " " << obji2._data << endl;
cout << objc1._data << " " << objc2._data << endl;
}