Make a const, and there's not problem with it. I don't believe it's
valid as written, but can't find chapter&verse to back me up.
I'm not sure if the language allows the second one, but again, I think
it's bad.
Comeau complains about both. Noting that the first case you need an
lvalue on the right. The second, "expression must be an lvalue or
function designator". Since B() is an rvalue, that would do it.
Thank you for the reply.
First I would like to clarify, that the codes were from an interview,
so no one would really write crappy code like this. But I know there
is something wrong, but I would like to find it out.
I tested the codes in VC6.0. No error message was issued, nor was
there any warning message. But I believe that you are right in that
the reference/pointer should be const, as temporaries cannot be
lvalue.
According to VC6.0,
A & a = B();
'a' would then reference to an (temporary) object. It works just fine
as if 'a' was declared as an object of class B. 'a' is destructed when
the scope closes.
However, for A * b = &B();
the destructor was invoked soon after the line was executed, which
means that 'b' points to a memory while the object no longer exists.
wandering if the lines of codes has compiler-dependent results.