为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C
开心每一天1111 2022-07-22 16:15:16
程序是这样的: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()是这个类的成员函数

查看完整回答
反对 回复 2022-07-24
?
尚方宝剑之说

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 名字。


查看完整回答
反对 回复 2022-07-24
  • 2 回答
  • 0 关注
  • 212 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号