neath@solar-1.stars.flab.Fujitsu.JUNET (06/12/89)
I am trying to understand the workings of pointer vs. reference. Specifically, the following piece of code declares a typedef for char*, then proceeds to use a reference to this typedef as a function argument type. This works fine and is accepted by <<cfront 1.2.1 2/16/87>>: #include <stream.h> typedef char* charP; void bar (const charP& c) { cout << form ("%s\n", c); } void foo (void) { bar ("HELLO WORLD"); } Now, what does it really mean to have a reference to a pointer to a thing in memory? Do I really get two levels of indirection here? If this is allowed by cfront, why can't I use a "char*&" instead of having to declare the typedef of charP? At the least, whatever the rule, it should be consistently followed. Regards Martin Neath <neath@dsg.ti.com> ------------------------------------------------------------------------ DISCLAIMER: As always, the opinions expressed above are strictly my own and do not reflect those of my employer, Texas Instruments. ------------------------------------------------------------------------
ark@alice.UUCP (Andrew Koenig) (06/14/89)
In article <NEATH.89Jun12170832@solar-1.stars.flab.Fujitsu.JUNET>, neath@solar-1.stars.flab.Fujitsu.JUNET writes: > #include <stream.h> > typedef char* charP; > void bar (const charP& c) { > cout << form ("%s\n", c); > } > void foo (void) { > bar ("HELLO WORLD"); > } > Now, what does it really mean to have a reference to a pointer > to a thing in memory? It means the same kind of thing it means to have a reference to any other type. In this case, when bar is called, c is bound to (a memory cell that contains) a pointer and can be used just as if it were a pointer. Incidentally, the equivalent form void bar (const char*& c) { cout << form ("%s\n", c); } is also legal. C++ 1.2 doesn't accept it because of a bug. C++ 2.0 does accept both forms. -- --Andrew Koenig ark@europa.att.com