c++ - C++ 中的 'string()+char'

标签 c++ string

我的问题是从关于 this 的评论中得到启发的所以发帖。

string()+char,如 string()+'a' 究竟是如何工作的?具体来说:

  1. string() 构造函数是否为无名临时对象调用?
  2. 这个对象的数据类型是什么?
  3. 从逻辑上讲,我如何将 char 连接到此对象以获得 string

最佳答案

假设您正在使用 std::string 实现,快速查看 C++ 引用中的相关函数可以帮助您理解这种行为。

  1. Is the string() constructor called for a nameless temporary object?
  2. What is the datatype of this object?

调用构造函数string()没有参数

Constructs an empty string, with a length of zero characters.

因此给定对象的数据类型将是 std::string 类型。

  1. Logically, how can I concatenate a char to this object to get a string?

您可以像连接任何字符串一样通过 + operator 进行连接,它被重载以将字符串对象作为左侧操作数,将字符作为右侧操作数。

关于c++ - C++ 中的 'string()+char',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50195756/

相关文章:

c - strcpy_s 是如何工作的?

python - 替换某些索引处的字母

c++ - 模板函数 : perform conversion based on typename

c++ - 如何在 C++ 中交叉包含类?

c++ - 警告 : 'auto' type specifier is a C++11 extension

c - 在函数 C 中传递并修改 char array[][]

C++ 服务器/客户端网络

c++ - 来自文件 C++ BorlandC 3.1 的字符串

c++ - 为什么在比较字符串时出现错误 "no match for ' operator= =' "?

java - 需要JAVA ME的字符串库