Consider the following code:
class B;
class A
{
public:
A() {}
A(B &b);
};
class B {};
A::A(B &b) {}
int main()
{
B b;
const A &refa = b;// does this line create a temporary value?
return 0;
}
My question is: does the code const A &refa = b; create a temporary value or not?
Yes, a temporary object
A(b)is created in the intialization statement, and it is immediately bound to the constant-referencerefa. This has the effect of prolonging the lifetime of the temporary to match the scope of therefavariable.