先看代码

#include<iostream>
#include<string>
using namespace std;

int main(int argc, char **argv)
{
  string s = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" + 'A';
  cout<<s<<endl;
  return 0;
}

很多人直觉认为应该输出 abcd....xyzA,因为字符串string 有重载 + 运算符,char类型和string类型concat。其实不然。

 string s = "abcd...xyz" + 'A'; 是调用的string的构造函数,它的参数是const char *,所以系统会认为右边先进行+运算,char 类型转成整形,对const char *进行指针便宜后的指针传给string的构造函数

所以结果应该是

 nopqrstuvwxyz 

相关文章:

  • 2021-06-03
  • 2021-11-27
  • 2021-07-31
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
  • 2021-11-09
  • 2018-05-07
相关资源
相似解决方案