C++中的typeid运算符 [英] typeid operator in C++

查看:45
本文介绍了C++中的typeid运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

int main()
{
    cout << "Please enter your name..." << endl;
    cin >> name;
    cout << "Data type = " << typeid(name).name() << endl;
    cin.get();
    return 0;
}

根据我读过的有关 typeid 运算符的各种教科书和文档,我应该阅读

According to the various textbooks and pieces of documentation I've read about the typeid operator, I should expect to read

"Data type = string"

作为输出.相反,我得到以下

as the output. Instead, I get the following

class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >

有人知道我哪里出错了吗?FWIW,我使用的是 Visual Studio 2010 Professional.

Does anyone have any idea where I'm going wrong? FWIW, I'm using Visual Studio 2010 Professional.

推荐答案

没有错.

那些教科书首先应该告诉你name()的结果是实现定义的,很可能是"".其次,该类型 std::string.std::string 类型只是 std::basic_stringchar 和朋友的 typedef.

Those text books, first of all, should have told you the result of name() is implementation-defined, and could very well be "". Secondly, that type is std::string. The std::string type is just a typedef of std::basic_string with char and friends.

这篇关于C++中的typeid运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆