recently I am learning C++ and have some doubt on the following case.
void function_a(const int &i){
//using i to do something
}
int function_b(){
return 1;
}
ok, if I am going to call…
function_a(function_b());
is there any chance that function_a read dirty reference from the it’s param?
Thank for your time.
In this case, the compiler will generate an unnamed temporary value whose reference will be passed to
function_a. Your code will be roughly equivalent to:The scope of
temporarylasts until the end of the statement that callsfunction_a()(this is inconsequential for an integer, but may determine when a destructor is called for a more complex object).