猿问

typeid(m_a).name(),这里为什么要加上name()

程序是这样的:
Comparetor(T a,T b):m_a(a),m_b(b){}
int compare(void){
cout<<typeid(m_a).name()<<endl;//这句话不明白
}

开心每一天1111
浏览 213回答 2
2回答

芜湖不芜

cout<<typeid(m_a).name()<<endl;//typeid 是c++语言开启了RTTI (运行时类型信息)的功能,这个功能开启c++会在每个类型上给个typeid 的类,name()是这个类的成员函数

尚方宝剑之说

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 名字。
随时随地看视频慕课网APP
我要回答