2015-12-01 34 views

回答

3

根据标准,这是一个实现定义空终止const char*

18.7.1类TYPE_INFO
....

const char* name() const noexcept;

返回:实现定义的NTBS

备注:该消息可以是一个空终止多字节字符串(17.5.2.1.4.2),适合于转化和 显示器作为wstring(21.3,22.4.1.4)

由于内容是实现定义的,它不能与其他字符串以可靠的方式进行比较,除非我们限于特定的实现。

0

当我尝试这个时,我得到了Ss。

#include <string> 
#include <typeinfo> 
#include <iostream> 

using namespace std; 

int main(int argc, char** argv) { 
    string str = "string"; 
    cout << typeid(str).name(); 
    return 0; 
} 

尝试:http://cpp.sh/4lsw

1

typeid(variable).name()returns指针空终止字符串,其可以使用strcmp()进行比较。但更好的方法来检查类型的变量是

if (typeid(a) == typeid(int)) 
+0

即使对于一个用户定义的数据类型,我们可以用这个吧? typeid(a)== typeid(class)其中a是类的对象。 –

+0

@AliAhsan,right –

+0

that help Thanks –

相关问题