What does -> mean in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rizzkhan
    New Member
    • Nov 2006
    • 2

    #1

    What does -> mean in C++

    Hello everybody,

    I am a bit confused about this operator -> could anybody explain this for me. The reason I am confused is that every document I read to get an explaination about it, explain it in different way which has created confusion in my mind. So, are there many uses of this operator or is it used only for one purpose. e.g, what would the following statement mean:

    app->displayAnyErro r(err);

    or e.g the following:

    basicApp()->uiManager()->addForm( this );

    Any kind of help and support would be appreciated.

    Riz
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Consider a structure Student
    Code:
    // define structure type to hold student information 
    struct Student {                       
                    char name[20];        // student name 
                    int age;              // student age 
                   };
    
    Student student,                 // define a variable to hold student data 
            *p_student = &student;   // define a pointer to the structure
    This defines type Student, variable student of type Student and p_student a 'pointer to type Student' initialised to point to student. The pointer can be used to access the members of the structure, e.g. to set up the age and name of the student:
    Code:
         (*p_student).age = 20;
         strcpy((*p_student).name, "daisy");
    Because the dot (.) operator has a higher precedence than the indirection operator (*) the parentheses are required to force the correct association using p_student as the pointer, i.e. the expression *p_student.age is using the structure member p_student.age as though it is a pointer to something. The use of pointers with structures is very common and an equivalent simpler notation using the -> structure pointer operator is generally used:
    Code:
         p_student->age = 30;
         strcpy(p_student->name, "mary");

    Comment

    • rizzkhan
      New Member
      • Nov 2006
      • 2

      #3
      Thanks horace for the explaination and such a quick response,

      Now I got an idea. But I noticed that when u used the dot (.) operator u used indirection operator (*) with the pointer variable p_student. While with the -> structure pointer operator you didnt use the indirection operator *. Is this a type or this is really the case?

      Waiting again for ur quick responnse.

      Riz

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by rizzkhan
        Thanks horace for the explaination and such a quick response,

        Now I got an idea. But I noticed that when u used the dot (.) operator u used indirection operator (*) with the pointer variable p_student. While with the -> structure pointer operator you didnt use the indirection operator *. Is this a type or this is really the case?
        Waiting again for ur quick responnse.

        Riz
        p_student->age is shorthand for (*p_student).ag e

        Comment

        Working...