simple examples of using "->"

Hello,

I'm learning C++ & stuck at ->
May I see some simple examples of using -> please.
And anything more to know about ->

(Sorry if the question is unspecific)

Thanks.
A sinple example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
   struct A
   {
      int x;
      int y;
   };

   A *pa = new A();

   pa->x = 10;
   pa->y = 20;

   std::cout << "A::x = " << pa->x << ", A::y = " << pa->y << std::endl;

   delete pa;
}
Last edited on
A more compound example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
   const int N = 10;
   struct A
   {
      int x;
      int y;
   } a[N];

   int initial = 10;
   for ( A *pa = a; pa < a + N; ++pa )
   {
      pa->x = initial;
      pa->y = initial / 2;
      initial *= 2;
   }

   for ( const A *pa = a; pa < a + N; ++pa )
   {
      std::cout << pa->x << ", " << pa->y << std::endl;
   }
}
@ cOde5: You know when you have a class or struct, and you can access members with the dot?
person.name = "Bob";

How would you do this if instead of an object you'd have a pointer?
1
2
3
// pointerToPerson.name = "Bob"; // doesn't work!
(*pointerToPerson).name = "Bob"; // method one
pointerToPerson->name = "Bob"; // method two 

Hello all,
Thanks for the helps. It's much more easier now.
Specially, this one:

// pointerToPerson.name = "Bob"; // doesn't work!
(*pointerToPerson).name = "Bob"; // method one
pointerToPerson->name = "Bob"; // method two


I will do more practice.
Topic archived. No new replies allowed.