What is this?

closed account (365X92yv)
" -> " What's this supposed to do?

Here's some context.

1
2
	now.hour = timeinfo -> tm_hour;
	now.min = timeinfo -> tm_min;
Last edited on
When you have a pointer to a class or a struct, and you want to access the member data or the member functions, you use the -> operator instead of the dot operator "."
There is absolutely no difference in their behaviour. One is used with pointers, and the other is used for uhh...non-pointers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Something
{
   int value;
   Something(int number)
   {
      value = number;
   }

   int getValue()
   {
      return value;
   }
};

int main()
{
   Something S (5);
   S.getValue(); //because S is not a pointer, we use the dot operator
   Something* pointerS(5);
   pointerS->getValue();//because pointerS is a pointer, we use the -> operator.
}
Last edited on
I felt that I should add that x->y is shorthand for (*y).x
In order to reference member functions/variables using pointers you first need to derefer the pointer. Because the '.' operator has a higher priority than the '*' operator you also need to put a parenthesis around the variable you're using.

See: http://cplusplus.com/doc/tutorial/pointers/
Topic archived. No new replies allowed.