What does -> mean?

This is a discussion on What does -> mean? within the C++ Programming forums, part of the General Programming Boards category; My book (The complete Idiots Guide to C++) never used it or explained it. eg: g_glRender->Init(); TIA...

  1. #1
    Registered User jimboob's Avatar
    Join Date
    Jun 2004
    Posts
    40

    Question What does -> mean?

    My book (The complete Idiots Guide to C++) never used it or explained it.
    eg: g_glRender->Init();

    TIA

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    It's syntax sugar for (*g_glRender).Init(). You can also define an operator->().

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    -> is the pointer operator.

    Kuphryn

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    its a pointer operator to a variable or function in a class
    eg.
    Code:
    class person
    {
      public:
         int number;
    };
    int main()
    {
      person loser;
      person *ptrLoser;
      ptrLoser=&loser;
      ptrLoser->number=10;
      return 0;
    }
    Woop?

  5. #5
    Registered User jimboob's Avatar
    Join Date
    Jun 2004
    Posts
    40
    Cool Thanks Everyone

  6. #6
    Stinking it up. StinkyRyan's Avatar
    Join Date
    Jun 2004
    Posts
    61
    You use it when dereferencing an object pointer:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class person
    {
    public:
    int age;
    };
    
    void grow (person* guy);
    
    int main(int argc, char *argv[])
    {
    
    person ryan;
    /* makes my age 17 */
    ryan.age = 17;
    
    cout<<"Ryan was, "<<ryan.age<<" years old."<<endl;
    
    /* sends the address of me off to grow */
    grow(&ryan);
    
    cout<<"Ryan was, "<<ryan.age<<" years old."<<endl;
    
      return 0;
    }
    
    void grow(person* guy)
    {
    guy->age = 18;
    /* note that (*guy).age = 18; would work here. */
    }
    the reason you can't just do:
    Code:
    *guy.age = 18;
    is because the period is evaluated before the *. So you'd have to use parentheses to alter the order of operations. Alas making you use the:
    Code:
    (*guy).age = 18;
    Although that is rather ugly, and not the easiest thing to work with, so c++ has the -> operator. which is the exact same thing.
    Code:
    (*guy).age = 18; == guy->age = 18;
    Because I can't.

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    (pointer to a struct or class) -> (class member function struct/class attribute)

    " -> " is also knows as the, "member selection operator" FYI

    useless trivia
    Last edited by The Brain; 07-18-2004 at 06:43 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Quote Originally Posted by jimboob
    What does -> mean?
    Over there.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. small -> big -> bigger -> bigger than bigger -> ?
    By happyclown in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-11-2009, 12:12 PM
  2. Dev-C++ -> Tools - > Editor -> Syntax
    By Yuri2 in forum C++ Programming
    Replies: 19
    Last Post: 07-03-2006, 07:48 AM
  3. > > > Urgent Help < < <
    By CodeCypher in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 01:06 PM
  4. electricity > AC circuits > tesla coil
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 03-14-2002, 01:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21