The address.
A reference is implemented the same as a pointer, it just has different rules.
The address.
A reference is implemented the same as a pointer, it just has different rules.
Another good book on the c++ language is the c++ all-ni-one deskreference for dummies. Its in the "for dummies" line of books (you know, the yellow and black ones) I have found it very helpful and an easy read
Cat,
So, a reference is implemented as some sort of constant pointer in such a way that you can't get at the address? And, all this talk of pass-by-reference is a hoax?
Sometimes, sometimes not.
References and pointers have a different interface, they have different syntax and different rules, but the underlying implementation is similar.
It's not really a "hoax", they are two different ways of using the same underlying idea (passing the address of a variable). Each has its uses; because of the different syntax, references can do things you can't do with pointers, and vice versa.
Also, although a reference is passed to and from functions like a pointer (by passing the address), within a function it takes up no additional space; it acts like an alias to the same memory location.
Last edited by Cat; 08-21-2003 at 09:07 PM.
It's not really a "hoax", they are two different ways of using the same underlying idea
It seems to me either it's passed-by-value or it's not. A pointer is passed by value to a function, i.e. a copy is made for the function, and the function uses that copy. If a reference is also passed by value then a copy is made for the function, and it seems to me it has to take up some space. If a reference isn't copied, then a reference should be more efficient than a pointer since no copying is necessary.
But an address still needs to be passed. The function needs to know where the object is located. Unless the function is inlined, there will be only one binary version of the function, and if it's to operate multiple times, then it has to be able to accept a parameter. This parameter must be passed in the traditional fashion (usually by being pushed to the stack), and so the method is to push the address of a reference parameter.
In fact, a pointer passed to a function or a reference passed to a function will likely generate identical code.