C++ Template之技巧性基础知识 和 typeid(x).name()用法

本文详细介绍了模板类、模板函数的高级用法,包括类型推断、成员模板、模板的模板参数、字符串作为函数模板实参等概念,并通过示例代码进行了深入解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.对于T是自定义类型的,如果存在子类型则需要在模版内部加上typename

示例代码:

1
2
3
4
5
template < typename  T>
class  Myclass
{
     typename  T::SubType *ptr; //需要加上typename不然编译器可能误解为是静态对象SubType和ptr相乘
};

2.类模版里对基类成员函数的调用使用BASE::exit();和this->,避免调用的是外部全局函数,但是在vc6.0上面这条规则是先调用的BASE里面的函数。

示例代码:

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
40
#include <iostream>
#include <string>
#include <vector>
using  namespace  std;
 
 
void  exit ()
{
     cout <<  "hello world"  <<endl;  
}
template  < typename  T>
class  BaseMyclass
{
public :
     void  exit ()
     {
         cout <<  "Base"  <<endl; 
     }
};
 
template  < typename  T>
class  Myclass: public  BaseMyclass<T>
{
     
public :
 
     void  foo()
     {
         exit ();
     }
};
 
 
int  main()
{
Myclass< int > m1;
m1.foo();
     
     return  0;
}

 3.成员模板,由于两种不同的类型之间的赋值不能使用类本身的接口,所以需要重新设计接口。

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <vector>
using  namespace  std;
 
template  < typename  T>
class  Stack
{
     public :
         template  < typename  T2>
         Stack<T>& operator=(Stack<T2>  const  &);
};
template  < typename  T>
template  < typename  T2>
Stack<T>& Stack<T>::operator=(Stack<T2>  const  &op2)
{
 
}
 
int  main()
{
     
     return  0;
}

 调用时首先通过显式指定的模板类型实例化一个类,然后通过实例化一个成员函数,只是现在的成员函数是模板函数,然后根据调用的参数实例化成一个成员函数。

4.模板的模板参数,实际上是一个模板类,上面的成员模板实际上是一个模板函数。

代码示例:

1
2
3
4
5
6
template  < typename  T, template < typename  T>  class  CONT = vector>
class  Stack
{
     public :
     
};

5.零初始化,因为模板不知道类型所以我们无法用任何一个常量来初始化模板参数定义的变量。但是可以如下例子进行初始化

代码示例:

6.使用字符串作为函数模板的实参若是引用则需要字符串长度完全匹配

示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template  < typename  T>
inline  T&  const  max(T  const &  a,T  const &  b)
{
     return  a > b ? a:b;
}
 
 
 
int  main()
{
     cout << max( "apple" , "peach" )<<endl; //OK
     cout << max( "apple" , "tomato" )<<endl; //ERROR
     return  0;
}

 若不为引用则不需要字符串长度完全一样,原因是参数会转换成字符串指针类型,属于相同的类型

示例代码:

 字符串作为实参的测试示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template  < typename  T>
void  ref(T  const & x,T  const & y)
{
     cout <<  "x int the ref is "  <<  typeid (x).name() <<endl;
}
 
template  < typename  T>
void  nonref(T x)
{
     cout <<  "x int the noref is "  <<  typeid (x).name() <<endl;
}
int  main()
{
     ref( "hello" );
     nonref( "hello" );
     
     return  0;
}

 

FROM: http://www.cnblogs.com/liuweilinlin/p/3213731.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值