views:

308

answers:

3

Possible Duplicate:
Why does C have a distinction between -> and . ?

Lets say that I have this structure:

struct movies
{
    string title;
    int year;
} my_movie, *ptrMovie;

Now I access my_movie like this: my_movie.year = 1999;
Now to access a pointer I must do this: ptrMovie->year = 1999;

Why do pointers use the -> operator and normal data types use the . operator? Is there any reason they couldn't both use the . operator?

+3  A: 

The . operator is only valid for a struct or class. A pointer is not a struct or class, so you need to dereference your pointer to get the struct/class it is pointing to like this

(*ptrMovie).year

The member operator . has a higher precedence than the dereference operator *, so you need to enclose the dereferencing operation in parenthesis. Or you could do this

ptrMovie->year

Both are equivalent. The '->' operator is a shortcut for dereferencing your pointer and then accessing a struct member. It is less typing and a little nicer to use in my opinion. Apparently most people agree with me because that is the standard way to access struct members from a pointer to the struct in most code that I've seen. You especially appreciate the difference when you have to do multiple levels of indirection:

ptrToStruct->memberPtr->subMemberPtr->subsubPtr->subsubsubPtr->x

(*(*(*(*(ptrToStruct).memberPtr).subMemberPtr).subsubPtr).subsubsubPtr).x

Both of those statements are equivalent, but the first is easier to work with.

A. Levy
". is only valid for a struct"??? really? how about classes?
Glen
Aside from the default protection (public for struct, private for class), class and struct are interchangeable in C++.
KeithB
@KeithB, yes, I know. However the answer implies otherwise. Maybe my comment wasn't particularly clear while trying to clarify that
Glen
Yeah...I should have said "struct or class" Even though they are functionally the same thing in C++.
A. Levy
+2  A: 

If they both used . how could you differentiate between the pointer and the actual object? To me:

->

Reminds me of an arrow which points to something, so I find it great that -> is used.

Instead of typing (*myPointer). it is simplier to use myPointer->

JonH
+9  A: 

The . operator accesses a member of a structure and can operate only on structure variables. If you want to do this to a pointer, you first need to dereference the pointer (using *) and then access the member (using .). Something like

(*ptrMovie).year = 1999

The -> operator is a shorthand for this.

Noufal Ibrahim
"The . operator accesses a member of a structure and can operate only on structure variables."??? really? how about class member variables?
Glen
I stand corrected. The OP mentioned structures and I confined my response to C (which doesn't have classes).
Noufal Ibrahim
@Noufal, yes, but the question is tagged C++ which has both structs and classes
Glen
Glen, there's no difference between a structure and a class outside their definition.
avakar
In C++ a structure is a type of class. `.` operates on all types of class including structures and unions.
Charles Bailey
@Glen. Yup. My mistake. I'm not very familiar with C++ and that narrowed my answer as well. Your comment clarifies the point though. Thanks.
Noufal Ibrahim
@avakar, Charles, yes, I know that. The answer however states that the . operator can only be used on structs. This is obviously incorrect, so I'm just trying to clarify the answer for others.
Glen