3

Possible Duplicate:
what is the difference between (.) dot operator and (->) arrow in c++

in this book i have I'm learning pointers, and i just got done with the chapter about OOP (spits on ground) anyways its telling me i can use a member selection operator like this ( -> ). it sayd that is is like the "." except points to objects rather than member objects. whats the difference, it looks like it is used the same way...

Community
  • 1
  • 1
TimothyTech
  • 745
  • 2
  • 11
  • 18
  • 4
    Duplicate of [what is the difference between (.) dot operator and (->) arrow in c++](http://stackoverflow.com/questions/1238613/what-is-the-difference-between-dot-operator-and-arrow-in-c) (and others) – James McNellis Jun 10 '10 at 23:58
  • If E1 has the type “pointer to class X,” then the expression `E1->E2` is converted to the equivalent form `(*(E1)).E2`. In other words, `->` is just a shortcut for "dereference-and-access". – GManNickG Jun 10 '10 at 23:59

7 Answers7

8

Where:

Foo foo;
Foo* pfoo = &foo;

pfoo->mem is semantically identical to (*pfoo).mem.

Or, put another way: foo.mem is semantically identical to (&foo)->mem.

greyfade
  • 24,948
  • 7
  • 64
  • 80
  • 5
    It's semantically identical. It would be syntactically identical if it was the same sequence of tokens :) – Pavel Minaev Jun 10 '10 at 23:59
  • 1
    `foo.mem` === `(*(&foo)).mem` :-) – Franci Penov Jun 11 '10 at 00:02
  • This is indirectly confusing since some of the foos in your examples are pointers and others are objects. – James McNellis Jun 11 '10 at 00:04
  • 1
    Did I fix all of the pedantic boo-boos? – greyfade Jun 11 '10 at 00:49
  • 1
    Not quite: `operator->` and unary `operator*` and `operator&` can be overloaded, but `operator.` can't. So your equivalences are necessarily true only when `pfoo` is a pointer. Other types that support your stated expressions may or may not implement the operators to be equivalent. You did ask ;-) – Steve Jessop Jun 11 '10 at 00:54
  • @Steve Jessop: Fair enough. I'm aware of this, but for the purposes of the question, my answer seems good enough. – greyfade Jun 11 '10 at 00:59
  • I agree the answer is good enough :-) Just that for pedantry, you could state the types of `foo` and `pfoo`. It's reasonable given the question that `pfoo` is of type `decltype(foo) *`, though, and overloading `operator&` breaks 99% of generic code anyway... – Steve Jessop Jun 11 '10 at 01:11
6

Yeah, it actually does the same thing but for different kind of variables.

If you have a pointer you have to use ->, while if you have a real value you will use ..

So for example

struct mystruct *pointer;
struct mystruct var;

pointer->field = ...
var.field = ...

That's not hard at all. Just remember that with a pointer you will need ->, and . otherwise.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • thank you very much. i thought so but my mind is pudding right now. so if the line has a pointer in it use -> or else use a . – TimothyTech Jun 11 '10 at 00:02
5

You only use -> when the variable is a pointer to your object:

A* a = new A;
a->member();

Use "." when it's not a pointer:

A a;
a.member();
zdan
  • 28,667
  • 7
  • 60
  • 71
1
struct S
{
    int a, b;
};

S st;
S* pst = &st;
st.a = 1;    // . takes an object (or reference to one)
pst->b = 2;  // -> takes a pointer
dan04
  • 87,747
  • 23
  • 163
  • 198
1

When you have an object instance (MyObject object;), you use the . to access it members (methods, properties, fields, etc), like this: object.Member.

When you have a pointer to an object instance (MyObject* pObject = new MyObject();), you need to dereference the pointer, before you can access the object members. To dereference a pointer you use the * operator. Thus, when you combine both, you get something like this: (*pObject).Member.

Of course, this is not readable, so the compilers take the -> as a shorthand for it. Thus, it becomes pObject->Member.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
0

maybe this example will help

object o;
object *p = &o;  // pointer to object
o.member;   // access member
p->member;  // access member through pointer
Anycorn
  • 50,217
  • 42
  • 167
  • 261
0

You use -> to dereference the pointer, when you access members via pointer, you use . when you access directly on member.

class A
{
   public:
   int x;
   void g() {};
};

A a;
a.x
a.g();

A * ap = new A();
ap->x;
ap->g();

and you can dereference pointer and then use .:

(*ap).x;
(*ap).g();
stefanB
  • 77,323
  • 27
  • 116
  • 141