将字符转换为字符串的代码行 --> foobar( "text" )+otherString)
line of code converting char to string --> foobar(string("text")+anotherString)
我有一行代码似乎可以将char数组转换为字符串
foobar(string("text")+anotherString);
foobar需要一个std::字符串作为参数
我从来没有见过这样的转换。。。对"text"调用了什么函数。或者这是一种棘手的选角方式?
std::string
有一个构造函数,它接受一个char数组*(假定为null终止),我相信您以前见过:
std::string s1 = "hello world";
std::string s2("also hello world"); // "same thing", essentially
所以std::string("test")
只是创建一个值为"test"
的临时字符串对象。
此外,对于string
和const char *
,还有一个空闲的operator+
,它将char数组中的数据(再次假定为null终止)附加到字符串中。
等效地,您可以编写std::string("test").append(anotherString)
,以获得相同的效果(即,包含两个字符串的临时字符串,连接在一起)。
有关std::string
支持的操作列表,请参阅任何合适的手册。
*),或者更确切地说,"指向字符数组第一个元素的指针"
string("text")
通过调用其构造函数并将其传递给"text"
来创建一个临时对象字符串。+anotherString
部分调用临时创建的"operator+"成员函数,该函数也返回一个字符串对象。最后,调用foobar并传递后一个字符串对象。
它与完全相同
string temp("text");
temp += anotherString;
foobar(temp);
如果这有助于
相关文章:
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- C 字符串比较“祝您好运”&gt;“再见”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 是否需要使用 - &gt;运算符在C 中调用成员函数时
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- ``这个''不能用this-&gt;指针变量
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 使用shared_ptr<字符串>转换为一个无序集合<字符串>
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到