Possible Duplicate:
Why does C have a distinction between -> and . ?
What is the real reason for a programmer to have to differentiate between . and -> when accessing a member of an object?
void foo( Point &p ) {
p.x ;
p->y; // syntax error
}
void foo( Point *p ) {
p.x ; // syntax error
p->y;
}
I mean, one way or another, they both refer to an object, p.
-
Why do I have to bother checking every time what p is ? Can’t the compiler understand ?
-
Why haven’t they allowed it to accept
.syntax for both? That would be fine for stack objects too.
And if it is due to C-tradition,
- Why don’t they allow both
.and->accessors?
For more than 15 years, I have always humbly considered the compiler errors as being my fault !
Because
p->dactually means(*p).d. It does a dereference and then a member access. References behave like objects thus they don’t need dereferencing (they are also a C++ feature, while pointer are inherited from C); it has been kept that way for backwards compatibility.C++ is full of inconsistencies like this, but there’s usually no better choice to add new features and keep old code working. Rewriting the old code is not an option.