Jeff Schwab said:
Just as delete pReg leaves a pointer to an invalid address.
There is a difference - the pointer can be set to 0 and subsequently tested.
Indeed it is common to write stuff like:
if( !p ) p = new P();
There is no equivalent for references.
It seems to me that the difference between these two notations is mostly
aesthetic. The only "real" difference I see is that -> can be
overloaded, whereas . cannot. Either might be preferable, according to
circumstance.
The advantage of references is the gaurantee (in the absence of people dong
the above) that the reference is valid.
This is very helpful when dealing with analy retentive coding standards that
insist that you check all pointers for null on entry.
One advantage of pointers is that you can put them in an auto_ptr and forget
them. (Of course you could write an equivalent for your references).
Another is that you can use 0 to mean something - either as a parameter when
it usualy means 'use defaults' or as a member when it means 'not yet
initialized'
If somebody can come up with a part of the Standard that prefers one of
these notations to the other, I'll be interested to read it.
I have found a more serious argument: consider
AnyClass& reg=*new DerivedFromAnyClass();
The standard states that the implementation can (implementation defined)
bind to the AnyClass subobject of a temporary copy.
This would obviously be a disaster since the original would leak and delete
® would try to deallocate something on the stack.
P.S. The equivalent to
AnyClass& reg=*new AnyClass();
is
AnyClass * const pReg=new AnyClass();
not
AnyClass *pReg=new AnyClass();
Because the latter can be rebound unlike the reference