Can someone explain the "->" pointer?

This is a discussion on Can someone explain the "->" pointer? within the C++ Programming forums, part of the General Programming Boards category; Hi, I see this type of pointer alot, and it's annoying me..I can't figure it out. I've read about it, ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    4

    Can someone explain the "->" pointer?

    Hi, I see this type of pointer alot, and it's annoying me..I can't figure it out. I've read about it, but they only touched upon the subject. Yes, i know how classes and regular pointers work, so don't hold back if it relates to them. I'm still kinda new, obviously, so I'd prefer it'd be explained, but a link is better than nothing -_-.
    Last edited by Ryeguy457; 08-08-2002 at 08:08 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The -> operator is relevant to structures, classes, and unions only. When you create a regular instance of a struct for example, you would do this:
    Code:
    // To create
    T var;
    
    // To access
    var.item;
    The -> operator was added to simply the same type of access on a pointer:
    Code:
    // To create
    T *var = new T;
    
    // To access
    (*var).item;
    
    // Alternate access for simplicity
    // It is equivalent to (*var).item
    var->item;
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    simply a convenience

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    simply a convenience? I don't see how. It's pretty fundamental. otherwise you have no way of accessing members through a pointer. Aside from maybe dereferencing. ewwww....

    (*ptr).member; //yuk
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    Originally posted by FillYourBrain
    otherwise you have no way of accessing members through a pointer.
    Like you said...
    Code:
    (*ptr).member

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    wasn't really arguing there.

    Anyway, what you're saying is that "->" was only added to the language to hide dereferencing? that's kinda cool I guess. In Java they have only the dot operator and all "references" are used that way. So they actually hide which things are pointers as well as the dereferencing. Pretty freaky if you ask me.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Java ... scary, scary stuff

  8. #8
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Well you overload -> so maybe c++ is more "freaky" than java.

  9. #9
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by FillYourBrain
    simply a convenience? I don't see how. It's pretty fundamental. otherwise you have no way of accessing members through a pointer. Aside from maybe dereferencing. ewwww....

    (*ptr).member; //yuk
    but with -> you are derefrencing, just using a different symbol
    hello, internet!

  10. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    that was explained already
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 01:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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