Re: Life time of an object and Reference Var
maverick wrote:
Sorry for not defining g(). But let me do it as follows:
foo& g()
{
foo local;
return local;
}
and lets also add a member var to class Foo say i of int type--
class Foo
{
int i;
Foo ( int j = 0 ) : i(j){}
};
now let say,
int main()
{
Foo& ref = g();
ref.i = 10; //?????CORE???
}
but it never core dumps.
The fact that a program doesn't core dump when you run it does not prove
that it is correct. Your program accesses an object that does not exist
anymore and exhibits undefined behaviour. Undefined behaviour can mean
anything, including that it seems to be working. You apparently get away
with it because you have a toy program that doesn't do anything besides
creating and destroying a single automatic object. But if you do
something like this in a non-trivial program, you are likely to cause
totally unpredictable effects that can be very hard to track down.
--
Gerhard Menzl
#dogma int main ()
Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".
The information contained in this e-mail message is privileged and
confidential and is for the exclusive use of the addressee. The person
who receives this message and who is not the addressee, one of his
employees or an agent entitled to hand it over to the addressee, is
informed that he may not use, disclose or reproduce the contents thereof.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]