Arrows ->

I've been studying C++ for awhile but i still can't grasp the usage of arrows -> .. how to use it and why ? Thanks for teaching~ appreciate it
It's a combination of dereferencing and member access.
Instead of foo->bar you can write (*foo).bar.
the Arror(->) is for accessing some thing beyond a pointer.

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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include <iostream>
using namespace std;

class SomeClass
{
public:
       int mydata;
       bool someTruth;

       SomeClass()
       {
              mydata = 0;
              someTruth = false;
       }
};

int main()
{
        SomeClass mySomeClass;  // Not a pointer...
        SomeClass *pSomeClass;  // we defined the pointer.
        pSomeClass = new SomeClass; // we allocate the pointer.
   
        // accessing the stuff beyond the pointer.
        pSomeClass->mydata = 10; // we assigned mydata, the object's member, to 10.
        pSomeClass->someTruth = true;  // now we made someTruth, the object's member, to true
        // the previous too lines are basically similar to
        mySomeClass.mydata = 10;
        mySomeClass.someTruth = true;

        // agian accessing the member of the pointer of SomeClass
        if(pSomeClass->someTruth == true)
        {
               cout << "pSomeClass->someTruth was True" << endl;
        }

        // clean up the pointer
        delete pSomeClass;
        
        return 0;
}
thanks for the help, i kinda found the examples given from various google site from some discussion forums they also stated that it is just to shorten the typing like what Athar said

foo->bar is equivalent to (*foo).bar

i kinda understand the *foo but may i know what is the dot for between foo and bar ?

appreciate the guidance given~
the dot is to access the member of the class pointed by foo.
Thanks a lot guys, I at least grasp the idea of the usage already :)
Topic archived. No new replies allowed.