Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 函数参数字符串按引用传递 void打印(常量字符串和str){ cout_C++_Pass By Reference - Fatal编程技术网

C++ 函数参数字符串按引用传递 void打印(常量字符串和str){ cout

C++ 函数参数字符串按引用传递 void打印(常量字符串和str){ cout,c++,pass-by-reference,C++,Pass By Reference,是的,因为string有一个构造函数,它接受const char*(string(const char*s)),并且这个构造函数没有标记为explicit,编译器将构建与您所问的代码等价的代码。您的理解是正确的 在这种情况下到底发生了什么?我知道涉及隐式转换 同样的情况也会发生,但涉及隐式转换,而不是显式调用构造函数 但我不确定何时以及如何做到这一点 引自: 每当在不接受某种类型T1但接受某种其他类型T2的上下文中使用某种类型T1的表达式时,就会执行隐式转换 我的意思是,是否也创建了一个临时

是的,因为
string
有一个构造函数,它接受
const char*
string(const char*s)
),并且这个构造函数没有标记为
explicit
,编译器将构建与您所问的代码等价的代码。

您的理解是正确的

在这种情况下到底发生了什么?我知道涉及隐式转换

同样的情况也会发生,但涉及隐式转换,而不是显式调用构造函数


但我不确定何时以及如何做到这一点

引自:

每当在不接受某种类型T1但接受某种其他类型T2的上下文中使用某种类型T1的表达式时,就会执行隐式转换


我的意思是,是否也创建了一个临时字符串对象


是的。

同样的事情也会隐式进行。
void print(const string& str){
    cout << str <<endl;;
}

int main(){
    print(string("asdf"));
}