ITPub博客

首页 > 应用开发 > IT综合 > 类模板继承和部分具体化例子及非类型参数

类模板继承和部分具体化例子及非类型参数

原创 IT综合 作者:gaopengtttt 时间:2016-08-30 17:34:48 1 删除 编辑
模板类必须所有的方法定义放到一个文件中。因为模板类不是函数不能单独编译

template<typename T1,typename T2,int T3>
class myext:public mybase<T2>
模板定义两个类型参数T1,T2,定义个非类型参数T3,他在使用期间如同一个常数。这个T3可以用来处理动态数组,并且继承了模板类mybase并且将类型参数T2赋予给他
template<typename T2,int T3>
class myext<int,T2,T3>:public mybase<T2>
模板定义一个类型参数T2,定义一个非类型参数T3,那么这里的T2就代表他是泛型的,而不具体化,但是开始的T1变为了具体化的int,并且继承了模板类mybase并且将类型参数T2赋予给他

template<typename T1,typename T2,int T3>
void myext<T1,T2,T3>::set(const T1 &in)
这是第一个模板类定义方法的方法

template<typename T2,int T3>
void myext<int,T2,T3>::set(const int &in)
这是第二个模板类定义方法的方法

调用:
myext<string,string,4> test2("test","gaopeng"); 将匹配template<typename T1,typename T2,int T3> class myext:public mybase<T2>
myext<int,int,10> test1(1,1); 将匹配template<typename T2,int T3> class myext<int,T2,T3>:public mybase<T2>
具体化过后当然选择的方法是选择匹配程度最高的模板。
下面是具体的测试代码没有实际意义:


点击(此处)折叠或打开

  1. 头文件
  2. tmp.h
  3. #include<iostream>
  4. using namespace std;

  5. template<typename T1>
  6. class mybase
  7. {
  8.         private:
  9.                 T1 a;
  10.         public:
  11.                 mybase(T1 in)
  12.                 {
  13.                         a=in;
  14.                 }
  15.                 virtual void show(void) const
  16.                 {
  17.                         cout<<a<<endl;
  18.                 }
  19.                 virtual ~mybase(){}
  20. };

  21. template<typename T1,typename T2,int T3>
  22. class myext:public mybase<T2>
  23. {
  24.         private:
  25.                 T1 b;
  26.         public:
  27.                 myext(T1 in1,T2 in2):mybase<T2>(in2)
  28.         {
  29.                 b=in1;
  30.         }
  31.                 virtual void show(void) const
  32.                 {
  33.                         for(int i=0;i<T3;i++)
  34.                         cout<<b<<endl;
  35.                         mybase<T2>::show();
  36.                 }
  37.                 virtual ~myext(){}

  38.                 virtual void set(const T1&);
  39. };

  40. template<typename T1,typename T2,int T3>
  41. void myext<T1,T2,T3>::set(const T1 &in)
  42. {
  43.         b=in;
  44.         for(int m=0;m<T3;m++)
  45.         {
  46.                 cout<<"test"<<endl;
  47.         }
  48. }

  49. template<typename T2,int T3>
  50. class myext<int,T2,T3>:public mybase<T2>
  51. {
  52.         private:
  53.                 int b;
  54.         public:
  55.                 myext(int in1,T2 in2):mybase<T2>(in2)
  56.         {
  57.                 b=in1+100;
  58.         }
  59.                 virtual void show(void) const
  60.                 {
  61.                         for(int i=0;i<T3;i++)
  62.                                 cout<<b<<endl;
  63.                         mybase<T2>::show();
  64.                 }
  65.                 virtual ~myext(){}
  66.                 virtual void set(const int&);
  67. };
  68. template<typename T2,int T3>
  69. void myext<int,T2,T3>::set(const int &in)
  70. {
  71.         b=in+100;
  72.         for(int m=0;m<T3;m++)
  73.         {
  74.                 cout<<"test1"<<endl;
  75.         }
  76. }


点击(此处)折叠或打开

  1. main函数

  2. #include<iostream>
  3. #include"tmp.h"
  4. using namespace std;


  5. int main(void)
  6. {
  7.         cout<<"use specialization template"<<endl;
  8.         myext<int,int,10> test1(1,1);
  9.         test1.set(10);
  10.         test1.show();
  11.         cout<<"use general template"<<endl;
  12.         myext<string,string,4> test2("test","gaopeng");
  13.         test2.show();
  14. }
输出:
use specialization template
test1
test1
test1
test1
test1
test1
test1
test1
test1
test1
110
110
110
110
110
110
110
110
110
110
1
use general template
test
test
test
test
gaopeng

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7728585/viewspace-2124228/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
wxh gp_22389860 公众号 MySQL学习 书籍 深入理解MySQL主从原理

注册时间:2021-06-07

  • 博文量
    868
  • 访问量
    3495545