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?

Community
  • 1
  • 1
Bob Dylan
  • 4,393
  • 9
  • 40
  • 58
  • ... because it would be confusing? Different operation -> different operator. – jldupont Jan 07 '10 at 17:38
  • 4
    Duplicate: http://stackoverflow.com/questions/1813865/why-does-c-have-a-distinction-between-and – CB Bailey Jan 07 '10 at 17:39
  • Oops...it didn't show up when I searched for it. Sorry. – Bob Dylan Jan 07 '10 at 17:42
  • I suppose the 'duplicate' is a C question and not a C++ question but as much of the reason why C++ is grammar is like it is is because that's how C does it. Some answers in the other question also address C++ compatibility as well, though. – CB Bailey Jan 07 '10 at 17:44
  • To be fair, if we look at C++ instead of C, another reason is that `operator->` needs to be distinct from operator. for implementing smart pointer classes (`operator->` must be overloadable to thunk through to the pointee, but `operator.` must be separate and non-overloadable to allow accessing methods on the smart pointer object itself). – jamesdlin Jan 07 '10 at 19:18

3 Answers3

9

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
  • 71,383
  • 13
  • 135
  • 169
  • "The . operator accesses a member of a structure and can operate only on structure variables."??? really? how about class member variables? – Glen Jan 07 '10 at 17:43
  • I stand corrected. The OP mentioned structures and I confined my response to C (which doesn't have classes). – Noufal Ibrahim Jan 07 '10 at 17:46
  • @Noufal, yes, but the question is tagged C++ which has both structs and classes – Glen Jan 07 '10 at 17:50
  • 1
    Glen, there's no difference between a structure and a class outside their definition. – avakar Jan 07 '10 at 17:53
  • In C++ a structure is a type of class. `.` operates on all types of class including structures and unions. – CB Bailey Jan 07 '10 at 17:54
  • @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 Jan 07 '10 at 17:57
  • @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 Jan 07 '10 at 17:59
  • @Glen: The `class` keyword creates a struct. However, some classes are not structs, they are unions, and the member access operators are usable with those too. – Ben Voigt Jun 07 '12 at 19:40
4

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
  • 29,056
  • 6
  • 39
  • 56
  • 1
    ". is only valid for a struct"??? really? how about classes? – Glen Jan 07 '10 at 17:42
  • Aside from the default protection (public for struct, private for class), class and struct are interchangeable in C++. – KeithB Jan 07 '10 at 17:55
  • @KeithB, yes, I know. However the answer implies otherwise. Maybe my comment wasn't particularly clear while trying to clarify that – Glen Jan 07 '10 at 18:00
  • Yeah...I should have said "struct or class" Even though they are functionally the same thing in C++. – A. Levy Jan 07 '10 at 18:10
  • How about `union`? In fact, it's valid for all classes (which are types introduced by the class-key `class`, `struct`, or `union`). – Ben Voigt Jun 07 '12 at 19:39
2

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
  • 32,732
  • 12
  • 87
  • 145