tags:

views:

126

answers:

3

I just started studying C++, and I met this new guy: ->. I was wondering if it means something different than (.) or not, and if it does, what it is.

Can you answer that? I looked for it a bit, but I didn´t find anything to answer my question.

+6  A: 

If you start with a pointer to an object, use ->. If you start with a reference or direct value of class type, use .. If you use the wrong one, the compiler should give a pretty clear error message.

a->b is defined to be synonymous with (*a).b.

Except in the case of operator overloading, in which case -> and * must be overloaded separately, and . cannot be overloaded.

Potatoswatter
`*a` can also be overridden, but `a.` cannot.
Merlyn Morgan-Graham
@Merlyn yeah, fixed… the situation isn't really that asymmetrical.
Potatoswatter
this.value is not a valid expression.
Chubsdad
@chubsdad: where did that come from?
Potatoswatter
A: 

You use . when you're working with an object, and -> when you're working with a pointer to an object. -> 'dereferences' the pointer (i.e. provides access to the underlying object).

Will A
A: 

'->' just means a pointer deference first and then access the class member.

class foobar
{
public:
   int mynum;
};

int main()
{
  foobar foo
  foobar *pfoo = &foo;

  pfoo->mynum;
  //same as
  (*pfoo).mynum;
}
Victor T.