临时对象是什么?

来源:百度知道 编辑:UC知道 时间:2024/06/09 02:25:40

就是当调用函数时,计算机临时建立一个对象.并且在此函数结束时,会把此临时对象返回并注销它在计算机中所占空间.

在C++中,有三种情况会产生临时对象:
(1)值参,如 int fun(int lhs, int rhs);
(2)const引用,如 int fun(const int &lhs, const int &rhs);
(3)隐式转换,如 void fun(string & rhs);
调用时: fun("sgsdgsd");
或者:char *str = "sgsdg";
fun(str);

调用时都会生成一个string类型的临时对象。

应该是作为函数参数传入的时候的临时生成的对象吧。

临时文件