|
20鱼币
如题,直接上代码(注释说明一切)- #include <cstdio>
- #include <iostream>
- using namespace std;
- template<typename T>
- class Demo
- {
- public:
- Demo()
- {
- printf("Demo create %d!\n", sizeof(T));
- }
- void print()
- {
- printf("%d\n", sizeof(T));
- }
- void print2(T v)
- {
- cout << v << endl;
- }
- };
- class User
- {
- public:
- template<typename T> static Demo<T> demo;
- };
- int main()
- {
- // 下面两行会报LNK1120: 1 个无法解析的外部命令
- // User::demo<int>.print();
- // User::demo<int>.print2(1);
- // 即使执行了下面一行,依旧没有调用Demo类的构造函数
- User::demo<int>;
- getchar();
- return 0;
- }
复制代码
IDE:VS2017 C++14标准 |
|