<al*****@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
|
|
in*****@gmail.com wrote:
| >
al*****@gmail.com wrote:
| > > int& get()
| > > {
| > > static int m;
| > > int* p=&m;
| > > return p; // should it be *p or p?
| >
| > It needs to be *p. Otherwise it will not compile.
|
| thats true, but WHY?
Because the return type is a reference. A reference is a permanent alias
to a guarenteed-valid object, not a pointer to what *might* be an
object.
Whether i call you "Allen" or "al" makes no difference, i'm still
refering to you. Your nickname is not your mailing address. While
remembering the nickname does occupy space in my tiny brain, it beats
having to use a mailing address since i don't even know if a house is
found there.
That sounds dumb, doesn't it? Well thats about how dumb comparing a
pointer to a reference gets you.
|
| >
| > > }
| > > but can there be a case when pointers may be confused with
references?
| >
| > Not really?
| >
| > > The reason I am asking is because the pointer contains the address
of
| > > an object so its value is a reference.
| >
| > Huh?
|
| Technically a reference denotes the address of an object, right? If
| this address is stored in a pointer, why can't we use the pointer as a
| reference?
|
No, absolutely not. A pointer denotes an address, it does not
neccessarily point to an object (null pointer, invalid pointer).
Additionally, a pointer doesn't know if and when its pointing to
anything. In fact, the only truth a pointer knows is that it itself is
associated with a type. There is nothing dumber than a pointer.
Pointer == bugs
Pointer == evil
Dumb Ptrs were definitely created by the devil.
A reference is often implemented with a pointer but the compiler applies
special rules that convey it very unique and disireable characteristics.
a) a reference to an object is not allowed to refer to another object
(even of the same type). The relationship between the reference and the
target is quarenteed to remain unchanged without exceptions.
b) a reference must be initialized and must remain valid at all cost
(hence: extend an object's lifetime).
c) there is no such thing as a null reference (at least not in the sense
a pointer can be null). Creating or accesing a reference implies a valid
target. That too is guarenteed.
int& r; // illegal
int n;
int& r_n = n;
....
r_n = 4; // modifies n transparently since r_n *is* n
// the code in ... can't break that relationship
These characteristics mean that code which rely on references loose 90%
of the ugly, sordid and gratuitous bugs that dumb pointers bring to the
language.
Always, always prefer a reference whenever possible. Even when a dumb
pointer does the job perfectly, a reference is still a far, far better
choice.
I can safely state that most of the coders in here have an overwhelming,
itching obsession to convert each and every pointer they come accross
into a reference.
Some probably loose sleep over it. Actually, thats not really true.
Converting every single pointer that can be converted means that the
newly released code will not mysteriously cease to function the very day
the client starts using it.
To drive the point yet deeper: a reference is nothing less than a
runtime check. As an example:
void mutiply(double& a, double& b)
{
a *= b;
}
Except for min/max numerical limits in this case, its virtually
impossible to use that function by calling it with invalid/undefined
variables. The compiler would generate an error if it can't initialize
those 2 references. With dumb pointers, however, the runtime system is
utterly blind. In my opinion, a dumb pointer is assumed to be
null/invalid until proven otherwise. That disease cannot infect a
reference. So...
reference != pointer.
In fact, the only thing those 2 have in common is that they both occupy
memory space (and sometimes not the same amount of space).