字符串字面值如何转换成string对象?

来源:百度知道 编辑:UC知道 时间:2024/09/24 12:14:37
如有一个函数void fun(string &a)
如果我调用fun("hello");
这里是如何将"hello"转换为string对象的?
还有这里"hello"应该算是一个常量,fun的参数是否得加个const
void fun(const string &a)?

关键不是这里,真正的调用过程是
"hello"->string 调用string的构造函数string(char*)
构造了一个临时的string对象
c++中临时对象一般认为是不可修改的,所以需要const

加个中间变量
string strTemp = "hello";
你的fun就可以用了。

同上面说的,加const最好