tags:

views:

3645

answers:

13

What is the difference between the dot (.) operator and -> in C++?

+1  A: 

The . operator is for direct member access.

object.Field

The arrow dereferences a pointer so you can access the object/memory it is pointing to

pClass->Field
Johannes Rudolph
You mean Object.Field or pObject->Field I supose
Arkaitz Jimenez
+5  A: 

The arrow operator is like dot, except it dereferences a pointer first. foo.bar() calls method bar() on object foo, foo->bar calls method bar on the object pointed to by pointer foo.

Meredith L. Patterson
A: 

-> use when you have pointer . use when you have structure (class) when you want point attribute that belong to structure use . structure.attribute when want point to attribute that have refference to memory by pointer use -> :

pointer->method;
or same as:
(*pointer).method
Am1rr3zA
+2  A: 

The target. dot works on objects; arrow works on pointers to objects.

std::string str("foo");
std::string * pstr = new std::string("foo");

str.size ();
pstr->size ();
ezpz
+25  A: 

Dot operator can't be overloaded, arrow operator can be overloaded. Arrow operator is generally meant to be applied to pointers (or objects that behave like pointers, like smart pointers). Dot operator can't be applied to pointers.

EDIT When applied to pointer arrow operator is equivalent to applying dot operator to pointee (ptr->field is equivalent to (*ptr).field)

Tadeusz Kopec
+1 for bringing up a point that others have missed.
Brian
+1 ^ what he said
Ali Parr
+1 ^ what he said ;)
Stefano Borini
+1 ^ what he said
Mk12
+32  A: 

foo->bar() is the same as (*foo).bar()
http://stackoverflow.com/questions/221346/what-is-the-arrow-operator-synonym-for-in-c

SwDevMan81
+2  A: 
pSomething->someMember

is equavivalent to

(*pSomething).someMember
Tamás Szelei
A: 

It's simple, whenever you see

 x->y

know it is the same as

 (*x).y
Logan Capaldo
Except when it isn't, such as when -> is overloaded.
jmtd
When you overload -> you should also overload * such that this relationship holds. To do otherwise will introduce all sorts of confusion anyway.
Logan Capaldo
A: 

The . (dot) operator is usually used to get a field / call a method from an instance of class (or a static field / method of a class).

p.myField, p.myMethod() - p instance of a class

The -> (arrow) operator is used to get a field / call a method from the content pointed by the class.

p->myField, p->myMethod() - p points to a class

Samuel Carrijo
A: 

Note that the -> operator cannot be used for certain things, for instance, accessing operator[].

#include <vector>

int main()
{
   std::vector<int> iVec;
   iVec.push_back(42);
   std::vector<int>* iVecPtr = &iVec;

   //int i = iVecPtr->[0]; // Does not compile
   int i = (*iVecPtr)[0]; // Compiles.
}
gparent
Clearly not. Because "foo->" does not mean "(*foo)". It means "(*foo).". It also can't be used for addition, subtraction... ;)
jmtd
I don't see how that's relevant. member[0] also doesn't mean anything, however syntactic sugar transforms it into member.operator[](0) if applicable. It's noteworthy that -> will not allow you to do what most people generally expect to be able to.
gparent
in regards to that operator, I mean.
gparent
I would imagine iVecPtr->operator[](0) would work, though. The point being that the syntactic sugar that you site turns [0] into .operator[](0); it does not turn .[0] into .operator[](0).
Domenic
A: 

The -> is simply syntactic sugar for a pointer dereference,

As others have said:

pointer->method();

is a simple method of saying:

(*pointer).method();

For more pointer fun, check out Binky, and his magic wand of dereferencing:

http://www.youtube.com/watch?v=UvoHwFvAvQE

Ali Parr
+3  A: 

For a pointer, we could just use

*pointervariable.foo

But the . operator has greater precedence than the * operator, so . is evaluated first. So we need to force this with parenthesis:

(*pointervariable).foo

But typing the ()'s all the time is hard, so they developed -> as a shortcut to say the same thing. If you are accessing a property of an object or object reference, use . If you are accessing a property of an object through a pointer, use -> ;D

CrazyJugglerDrummer
A: 

(Dot) The target . dot work on object

(Arrow) Arrow work on pointer to the object

(Example of Dot)

(*pointer).method

(Example of Arrow)

pointer->method

Abdulsamad PHP Developer