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

2
  • 4
    Duplicate of what is the difference between (.) dot operator and (->) arrow in c++ (and others) Commented Jun 10, 2010 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". Commented Jun 10, 2010 at 23:59

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

7 Comments

It's semantically identical. It would be syntactically identical if it was the same sequence of tokens :)
foo.mem === (*(&foo)).mem :-)
This is indirectly confusing since some of the foos in your examples are pointers and others are objects.
Did I fix all of the pedantic boo-boos?
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 ;-)
|
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.

1 Comment

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 .
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(); 

Comments

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 

Comments

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.

Comments

0

maybe this example will help

object o; object *p = &o; // pointer to object o.member; // access member p->member; // access member through pointer 

Comments

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(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.