Temporary object passed by reference?

i just have a question. How do you pass a "temporary" object to a function that accepts its arguments by reference?
[115 byte] By [pheradise] at [2007-11-19 19:33:23]
# 1 Re: Temporary object passed by reference?
[ Split thread ]
Andreas Masur at 2007-11-9 0:57:14 >
# 2 Re: Temporary object passed by reference?
i just have a question. How do you pass a "temporary" object to a function that accepts its arguments by reference?

If the reference is const, any rvalue can be passed.

If the reference is non-const, you can create a local variable:

void Func(int& x)
{
/* do something which uses and modifies x */
}

int OtherFunc()
{
/* some code here */
{
int temp=FunctionWhichReturnsInt();
Func(temp);
/* Theorically, you should now use the new value of temp */
}
/* more code here */
}
SuperKoko at 2007-11-9 0:58:17 >
# 3 Re: Temporary object passed by reference?
You cannot pass a temporary to a function that accepts the respective argument by reference. It will and should not work. When you pass by reference, the argument object may be subject to change and temporaries are r-values. In the following if you remove the const-ness of the argument, it gives a compiler error (it becomes illegal to do so) otherwise it's fine as long as you don't try to modify the "a".

class A{};

void func(const A& a){
//some code...
}

int main(){
func(A());
}
exterminator at 2007-11-9 0:59:22 >