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.
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.
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.
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).
'->' 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;
}