我的问题是从关于 this 的评论中得到启发的所以发帖。
string()+char
,如 string()+'a'
究竟是如何工作的?具体来说:
string()
构造函数是否为无名临时对象调用?- 这个对象的
数据类型
是什么? - 从逻辑上讲,我如何将
char
连接到此对象以获得string
?
最佳答案
假设您正在使用 std::string
实现,快速查看 C++ 引用中的相关函数可以帮助您理解这种行为。
- Is the
string()
constructor called for a nameless temporary object?- What is the datatype of this object?
调用构造函数string()
没有参数
Constructs an empty string, with a length of zero characters.
因此给定对象的数据类型将是 std::string
类型。
- 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/