c++ - C++ 中的 typeid 运算符

标签 c++ visual-c++ typeid

我有以下代码

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

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

"Data type = string"

作为输出。相反,我得到以下信息

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

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

最佳答案

没有错。

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

关于c++ - C++ 中的 typeid 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3721078/

相关文章:

c++ - SDL/TTF : TTF_RenderText_Blended_Wrapped returns NULL

c++ - Isspace 字符函数

c++ - 打开/关闭 handle 时会发生什么?

C++ typeid 作为返回类型

c++ - 表示行为与重复代码块的函数参数?

c++ - 声明指针/引用变量的首选方式?

c++ - 如何在 C++ 中编译位于不同文件中的模板?

c++ - 检测我们是在子类还是基类

c++ - 什么时候使用 'typeid' 是最好的解决方案?

c++ - 初始化对象数组中的 vector 大小