Thread: What does "->" operator mean?

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    13

    Question What does "->" operator mean?

    Hi all;

    I was reading through the source code for Doom, and found an operator that I'm not familiar with. Despite some Google research, the only explanation I can find comes from a brief mention on Wikipedia saying that it's a Member by Pointer, with no other info.

    Does anyone know what this means? I'm very familiar with pointers, but don't know what Member by Pointer is. Sample code down below.

    In the source code, this operator is used all over the place, so it must be really important and useful.


    Thanks
    -Fremontknight

    Code:
     // set the entire zone to one free block
        mainzone->blocklist.next =
    	mainzone->blocklist.prev =
    	block = (memblock_t *)( (byte *)mainzone + sizeof(memzone_t) );
    
        mainzone->blocklist.user = (void *)mainzone;
        mainzone->blocklist.tag = PU_STATIC;
        mainzone->rover = block;
    	
        block->prev = block->next = &mainzone->blocklist;

  2. #2
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Edit: It's (*mainzone).blocklist, per laserlight. Sorry.

    (*foo).bar == foo->bar.

    Operator . binds tighter than operator *, per http://www.cppreference.com/operator_precedence.html. That's why it's (*foo).bar - to force * to come first, so to speak.
    Last edited by Lithorien; 05-07-2008 at 03:19 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, mainzone->blocklist is another way of saying (*mainzone).blocklist, not *mainzone.blocklist.

    *foo.bar is equivalent to *(foo.bar)
    foo->bar is equivalent to (*foo).bar
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    No offense, but I don't think you're ready for the source code of Doom or any other advanced piece of software if you have to ask this.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    Lithorien and Laserlight: thanks for the good answers....thank you.

    medievalelks: You're acting like an idiot. Keep your useless put-downs to youself. In my life, I've always learned the most by stretching myself and reading real code to improve my knowledge.
    Remember, you were once just like me.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Sorry for the bluntness of my remark, I just don't understand why so many people seem to eschew books when learning C++ nowadays. If we get one, we get a dozen questions daily that could be answered by looking at the most basic C++ book (or even online reference).

    Maybe it's a generational thing. I didn't have forums like this when I was learning, other than maybe Usenet. Even then, I'd ask questions as a last resort. Maybe that's where I got my acerbic nature - some of those Usenet guys can be *brutal* to newbies. Check out comp.lang.c++ for a few days and you'll think I'm Prince Charming :-)

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    13
    No prob medievalelks. Thanks to BitTorrent, I have about 200 C++ books, and I read them like a hungry insane man. Most books spend most time talking about classes and OOP, but don't talk about a nuance like the one I asked about. Not even my college textbook had mention of this question, and even Google didn't give a clear reply. Thank you, and to comp.lang.c++ I go. Take care...

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by Fremontknight1 View Post
    No prob medievalelks. Thanks to BitTorrent, I have about 200 C++ books, and I read them like a hungry insane man. Most books spend most time talking about classes and OOP, but don't talk about a nuance like the one I asked about. Not even my college textbook had mention of this question, and even Google didn't give a clear reply. Thank you, and to comp.lang.c++ I go. Take care...
    if you have an IDE the helpfiles are great for things like this. you just type in -> into the index and it will have an entry.

    things like style and the finer points of how to do things right...not so much. books are def. the way to go for that. unfortunately for me, i don't have the time at work ro read them or the desire to read them at home!

    i'm rather sophomoric for it.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I have about 200 C++ books, and I read them like a hungry insane man.
    I find it astonishing that not one of them ever explained -> to you.
    More that you were in such a hurry that it never sank in when you saw it.

    It is perfectly possible to be "fact heavy" on C++ and still be utterly clueless on how to program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Salem View Post
    > I have about 200 C++ books, and I read them like a hungry insane man.
    I find it astonishing that not one of them ever explained -> to you.
    I find it equally astonishing. One surely couldn't possibly not know what -> is if they've been programming for 2 weeks or more in one of the many programming languages that uses it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Another term for it is the "indirection operator". I use it in classes when I return data members.

    Code:
    int Foo::getNumber() const
    {
       return this->m_Number;
    }
    Although technically you dont have to use the this* pointer to return data members, its just somthing I do.
    Double Helix STL

  12. #12
    Banned
    Join Date
    Nov 2007
    Posts
    678
    but, the question is, why would anyone overload it for their class?

    medievalelks, how do i access comp.lang.c++ from Windows XP?
    sorry for such a stupid question, but i could never visit such sites(?) from my browser (Firefox or IE).

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    but, the question is, why would anyone overload it for their class?
    Perhaps their class is a smart pointer class.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by manav View Post
    medievalelks, how do i access comp.lang.c++ from Windows XP?
    sorry for such a stupid question, but i could never visit such sites(?) from my browser (Firefox or IE).
    You can read Usenet groups using groups.google.com, but there is an almost unbearable influx of spam posted to the unmoderated ones (mostly from gmail users trying to get Google Ad hits, ironically). comp.lang.c++.moderated takes care of that problem for C++ programmers, but that also means your post has to be approved by a moderator, so it takes longer to get there. It also means FAQs aren't usually posted, nor are off-topic Windows, UNIX, etc. questions.

    There are also comp.lang.c, comp.object, lots of unix and windows programming groups, and just about anything you can think of. Usenet has been around long before the WWW and browsing, and the archive on groups.google.com goes back at least to the early 80s the last I checked. It's a great place to research.

  15. #15
    Banned
    Join Date
    Nov 2007
    Posts
    678
    thanks medievalelks!
    i can access the comp.lang.c++ now, i suppose it is a usenet group, never used it before, only heard of it, somewhere.

    i wanted to see them getting really angry at stupid newbie questions, i could not find such posts
    tell me if you come across such postings!

Popular pages Recent additions subscribe to a feed