Path: utzoo!utgpu!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!ames!elroy!jpl-devvax!rich From: rich@jpl-devvax.JPL.NASA.GOV (Richard Pettit) Newsgroups: comp.lang.c++ Subject: Re: use of new allocator with reference variables Keywords: c++, new, reference Message-ID: <2732@jpl-devvax.JPL.NASA.GOV> Date: 26 Aug 88 18:04:23 GMT References: <646@paris.ICS.UCI.EDU> Reply-To: rich@jpl-devvax.JPL.NASA.GOV (Richard Pettit) Distribution: na Organization: Jet Propulsion Laboratory, Pasadena, CA. Lines: 47 > >Vec& sum = *new Vec(s); Lets start from the top. Reference is just generally confusing. Consider Pascal: (forgive the formatting) program plugh (output); var i : integer; procedure xyzzy (var a : integer); begin a := 3; end; begin xyzzy(i); end. Note that in this example, the value "3" will be "copied back" into variable "i" upon return from xyzzy. This is because "a" is a REFERENCE variable. Now look at the C++ version: void xyzzy(int &a) { a = 3; } main() { int i; xyzzy(i); } This does the same thing. The C version: void xyzzy(int *a) { *a = 3; } main() { int i; xyzzy(&i); } makes explicit use of the pointer. The REFERENCE notation in C++ simply eliminates the need for making pointers to everything. Variables which are made reference variables are treated like regular non-pointer variables (like Pascal). The C++ declaration: Vec& sum = *new Vec(s); is such because new "returns" a pointer. The REFERENCE variable "sum" is treated syntactically like a structure, and the value assigned to it must also be a structure. Hence the de-reference on the "new" allocator. Just as the notation Vec a; Vec &b = a; assigns "a reference to `a'" to "b", the previous statement assigns "a reference to an anonymous heap space variable" to "sum". I've probably confused the issue even worse. Rich -- "Macrosolutions to Megaproblems" -- Voivod rich@jpl-devvax.Jpl.Nasa.Gov