程序是这样的:Comparetor(T a,T b):m_a(a),m_b(b){}int compare(void){cout<<typeid(m_a).name()<<endl;//这句话不明白}
2 回答
芜湖不芜
TA贡献1796条经验 获得超7个赞
cout<<typeid(m_a).name()<<endl;//
typeid 是c++语言开启了RTTI (运行时类型信息)的功能,
这个功能开启c++会在每个类型上给个typeid 的类,name()是这个类的成员函数
尚方宝剑之说
TA贡献1788条经验 获得超4个赞
typeid 是运算符,检查 表达式的 类型:
typeid (表达式)
计算返回 type_info 型的 常对象地址,头文件 <typeinfo>里定义。.name() 返回类型名字。
书上例子:
#include <iostream>
#include <typeinfo>
usingnamespace std;
int main () {
int * a,b;
a=0; b=0;
if (typeid(a) != typeid(b))
{
cout << "a and b are of different types:\n";
cout << "a is: " << typeid(a).name() << '\n';
cout << "b is: " << typeid(b).name() << '\n';
}
return 0;
}
输出:
a and b are of different types:
a is: int *
b is: int
如果是 class , 它能返回 class 名字。
- 2 回答
- 0 关注
- 212 浏览
添加回答
举报
0/150
提交
取消
