【问题标题】:Why does the output comes out different as both of the c++ statements are doing the same operation为什么输出会不同,因为两个 c++ 语句都在执行相同的操作
【发布时间】:2021-09-09 22:00:08
【问题描述】:
string k="*";
cout<<typeid(k).name()<<endl;

输出:Ss

cout<<typeid("*").name()<<endl;

输出:A2_c

这两者之间有什么区别导致输出不同?以及输出的含义是什么?

【问题讨论】:

    标签: c++ string char cout typeid


    【解决方案1】:

    string k = "*" 中的kstd::string 类的一个实例,而"*" 是一个const char[2] 数组,因为它是string literalk是根据数组的内容构造的,但它本身并不是这样的数组。

    另外,请注意typeid()...

    ... 返回包含类型名称的实现定义的以空字符结尾的字符串。 不提供任何保证,特别是返回的字符串对于多种类型可能是相同的,并且在同一程序的调用之间会发生变化。

    更多信息,请查看this reference documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      相关资源
      最近更新 更多