Here is a simple snippet of C++ code:
A foo(){
A a; // create a local A object
return a;
}
void bar(const A & a_r){
}
bar(foo());
Why does the argument of function bar have to be a const reference,not just a reference?
Edit1: I know that reference is to avoid copying overhead. and const is for read-only. But here I have to make it a const reference, otherwise if I remove the "const", g++ will throw an error to me.
Edit2: My guess is that the return object of foo() is a temporary object, and it's not allowed to change the value of a temporary object ?