Re: Reference nightmare
On Apr 21, 3:56 am, Stefan Chrobot <jan...@op.pl> wrote:
Hello!
Temporary objects can be bound to const reference and the process
extends the lifetime of the temporary to the lifetime of the reference.
The question is whether taking const reference to that reference extends
the lifetime of the temporary (or the reference) any further?
I think the original question has been answered. On a side note,
consider this:
struct B{};
struct A
{
const B& ref;
A(const B& ref_) : ref(ref_){}
void func()
{
//using ref here;
}
};
int main()
{
A object_a(B()); //#1
object_a.func(); //#2
}
In this case, what you are expecting will happen. A's object is
constructed and the reference member is initialized taking a const
reference of the temporary. But that temporary ceases to exist after
the executing of statement #1 (after successful constructor call). Any
further access of member ref in A, for example in the function call
shown in #2 would be wrong in assuming that the temporary is still
bound to the member reference.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]