<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<head><meta forua="true" http-equiv="Cache-Control" content="max-age=0" /></head>
<card title="C++中如何判断template类型" id="card1">
<p> 游客</p><p>
标题:C++中如何判断template类型<br/>
正文:<br/>
在C++中，使用template，有时候可能会需要得到当前所使用的类型.本文中使用两种办法来。TT类为使用模板的类，TT.h#ifndef _TT_H<br/>#define _TT_H<br/>#include &quot;stdio.h&quot;<br/>template &amp;lt;class T&amp;gt;<br/>class TT{<br/>public:<br/>    void printType();<br/>    void printType(T);<br/>}; 方法1：template&amp;lt;&amp;gt;<br/>void TT&amp;lt;int&amp;gt;::printType(){<br/>    printf(&quot;int/n&quot;);<br/>}<br/>template&amp;lt;&amp;gt;<br/>void TT&amp;lt;char&amp;gt;::printType(){<br/>    printf(&quot;char/n&quot;);<br/>}<br/>template&amp;lt;class T&amp;gt;<br/>void TT&amp;lt;T&amp;gt;::printType(){<br/>    printf(&quot;other type/n&quot;);<br/>} 方法1结束。方法2：template&amp;lt;class T&amp;gt;<br/>void TT&amp;lt;T&amp;gt;::printType(T t){//判断t是不是int类型的<br/>    printf(&quot;%d/n&quot;,typeid(t).name() == typeid(1).name());<br/>}方法2结束。#endif<br/>main.c<br/>#include &quot;TT.h&quot;<br/>int main(){<br/>    TT&amp;lt;int&amp;gt; t1;<br/>    TT&amp;lt;char&amp;gt; t2;<br/>    TT&amp;lt;double&amp;gt; t3;<br/>    t1.printType();<br/>&amp;nbsp<br/><a href="http://camnpr.com/wap.asp?mode=WAP&amp;act=View&amp;id=1214&amp;Page=1">[&lt;&lt;]</a><a href="http://camnpr.com/wap.asp?mode=WAP&amp;act=View&amp;id=1214&amp;Page=1">[[1]]</a><a href="http://camnpr.com/wap.asp?mode=WAP&amp;act=View&amp;id=1214&amp;Page=2">[2]</a><a href="http://camnpr.com/wap.asp?mode=WAP&amp;act=View&amp;id=1214&amp;Page=2">[&gt;&gt;]</a><br/>
<br/>
<a href="wap.asp?act=Com&amp;id=1214">查看评论(0)</a><br/>
<a href="wap.asp?act=AddCom&amp;inpId=1214">发表评论</a><br/><br/>

<br/>

<br/>
<a href="http://camnpr.com/wap.asp">首页</a>
</p>
</card>
</wml>