I was just wondering why, when using object pointers, you must use the -> operatior. Why couldnīt you simply follow the standard method like: *p.funct() if you were accesing the funct() member through the *p pointer?
Thanks for the help
Printable View
I was just wondering why, when using object pointers, you must use the -> operatior. Why couldnīt you simply follow the standard method like: *p.funct() if you were accesing the funct() member through the *p pointer?
Thanks for the help
sure, you can do that - but because of operator precedence you'll need to put a parenthesis around the pointer dereference, ie: (*p).funct()
And it's a lot easier to just write -> than to write all the parentheses and *'s, let alone site there and figure out where they're all supposed to go.
Hello,
For example sake, let's create a structure called point:The following declaration says that p is a pointer to a structure of type struct point. If p points to a point structure, *p is the structure, and (*p).x and (*p).y are the members:Code:struct point {
int x;
int y;
};
To use p, we might write, for example:Code:struct point *p;
The parenthesis are necessary in (*p).x because the precedence of the structure member operator (.) is higher than (*). The expression *p.x means *(p).x, which is illegal here because (x) is not a pointer.Code:struct point *p;
printf("(%d %d)\n", (*p).x, (*p).y);
Pointers to structures are so frequently used that an alternative notation is provided as a shorthand. If (p) is a pointer to a structure, then the following code refers to the particular member:
(p)->member-of-structure
Note: Both (.) and (->) associate from left to right.
Edit: Takes me forever to post. Heh. :cool:
- Stack Overflow
Also, when you get into operator overloading, * and -> often will not mean "dereference" any more; rather, they will mean "dereference one of my member variables", in which case I personally find -> to be more intuitive - though I imagine that's totally up to you.