Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!columbia!caip!princeton!allegra!alice!ark From: ark@alice.UucP (Andrew Koenig) Newsgroups: net.lang.c Subject: Re: references - C++ Message-ID: <6035@alice.uUCp> Date: Tue, 9-Sep-86 10:08:40 EDT Article-I.D.: alice.6035 Posted: Tue Sep 9 10:08:40 1986 Date-Received: Wed, 10-Sep-86 01:26:46 EDT References: <6170@think.COM> Organization: Bell Labs, Murray Hill Lines: 28 Apparently I caught whatever was affecting Bjarne when he posted his original note about references. Sigh. I'll try again. Sam Kendall is right -- if you initialize a reference with something that isn't an lvalue then the reference refers to a temporary. Thus: char c; char &cr; /* illegal -- no initialization */ char &cr = c; /* legal */ char &cr = 'a'; /* equivalent to: char temp='a'; char &cr=temp; */ char &cr = &c; /* illegal -- &c is not a char */ Sam is also right in saying that initializers do not have to be explicitly provided for references declared as formal parameters. This is indeed because the initializers will appear whenever the function is called. Thus: void zap (int& x) { x = 0; } int z; zap (z); /* sets z to 0 */ However: double d; zap (d); /* illegal -- d is the wrong type */ zap ((int) d); /* sets a copy of the integer value of d to 0. Essentially no effect */