- // NoType.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- using namespace std;
- /*template <double VAT> // 错误:浮点值不能作为 template parameters
- double fun()
- {
- return VAT * 1.2;
- }
- template <string strName>
- class MyClass
- {
- public:
- MyClass(){m_strClassName = strName;}
- private:
- int m_nLen;
- string m_strClassName;
- };*/
- template <const char* szName>
- class MyClass
- {
- public:
- MyClass();
- ~MyClass();
- public:
- void Print();
- private:
- char* m_szClassName;
- };
- template<const char* szName>
- MyClass<szName>::MyClass()
- {
- m_szClassName = (char*)szName;
- }
- template <const char* szName>
- MyClass<szName>::~MyClass()
- {
- }
- template <const char* szName>
- void MyClass<szName>::Print()
- {
- cout << "class name = " << m_szClassName << endl;
- }
- extern const char strGlobal[] = "andylin";
- int _tmain(int argc, _TCHAR* argv[])
- {
- MyClass<strGlobal> obj;
- obj.Print();
- return 0;
- }
nontype template parameters 使用字符串作为template arguments
最新推荐文章于 2022-06-28 22:49:01 发布