views:

108

answers:

7

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...

+5  A: 

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
Simple answers are the best.
Gary Willoughby
A: 

maybe this example will help

object o;
object *p = &o;  // pointer to object
o.member;   // access member
p->member;  // access member through pointer
aaa
+5  A: 

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
It's semantically identical. It would be syntactically identical if it was the same sequence of tokens :)
Pavel Minaev
Franci Penov
This is indirectly confusing since some of the foos in your examples are pointers and others are objects.
James McNellis
Did I fix all of the pedantic boo-boos?
greyfade
Not quite: `operator->` and unary `operator*` and `operator-)
Steve Jessop
@Steve Jessop: Fair enough. I'm aware of this, but for the purposes of the question, my answer seems good enough.
greyfade
Steve Jessop
A: 
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
+1  A: 

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
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
A: 

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
+1  A: 

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