Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. QOTD

QOTD

Scheduled Pinned Locked Moved The Lounge
c++jsonquestionlearning
42 Posts 17 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Colin Leitner

    I'm sorry, I forgot they implemented the operator-> too (argh! operator overloading begins to suck ;)

    C Offline
    C Offline
    Colin Leitner
    wrote on last edited by
    #30

    Clickety.I choose a random iterator type, but they all behave the same.

    1 Reply Last reply
    0
    • D David Stone

      Congrats to Christian Graus for thinking up the QOTD. I believe this is the first one I've seen from a CPian. Of course, I can't answer because I haven't a clue when it comes to STL. So I'll just leave the answering of it to the rest of you. :) David Stone It seemed similar to someone saying, "Would you like to meet my knife collection?" Ryan Johnston on Elaine's sig

      G Offline
      G Offline
      Giles
      wrote on last edited by
      #31

      Ahh, my brain is switched off. For that past few days I've been going though interfaces/abstract classes and coding COM objects in pure C++, to gain a better understanding. Now I think I know this one, as I use them quite a bit, but I've not got the energy to make sure. brain is mush, and I've just finished watching Lord 'o' the Rings. I'm battered, and I think I'm going to have another glass of wine...... :-D(stupid grin)

      1 Reply Last reply
      0
      • D David Stone

        Congrats to Christian Graus for thinking up the QOTD. I believe this is the first one I've seen from a CPian. Of course, I can't answer because I haven't a clue when it comes to STL. So I'll just leave the answering of it to the rest of you. :) David Stone It seemed similar to someone saying, "Would you like to meet my knife collection?" Ryan Johnston on Elaine's sig

        T Offline
        T Offline
        Todd Smith
        wrote on last edited by
        #32

        What about option E) const_cast<Type&>(*it).Foo() ? Otherwise you're editing a copy of the object. Todd Smith

        1 Reply Last reply
        0
        • C Chris Losinger

          there's probably some deep metaphysical reason why people want to use (*it). instead of it-> . i just do it because that's how i learned it. Ryan Johnston wrote: What kind of programming do you do? a little of everything, but mostly 2d graphics. -c


          Conservative: One who admires radicals centuries after they're dead. -- Leo C. Rosten

          image effects!

          D Offline
          D Offline
          Daniel Lohmann
          wrote on last edited by
          #33

          Chris Losinger wrote: there's probably some deep metaphysical reason why people want to use (*it). instead of it-> . i just do it because that's how i learned it AFAIR it is just because early versions of STL and/or the C++ compiler used those days didn't support the "intuitive" syntax. In our days using it->Something should work quite fine. -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )

          1 Reply Last reply
          0
          • M Michael Dunn

            peterchen wrote: What's wrong with 'it' a You mean, aside from being incorrect code? ;) An iterator is an opaque data type, like say POSITION in MFC. You can't make any assumptions about it actually being a pointer to the underlying data, because iterator is not documented as such. --Mike-- Just released - RightClick-Encrypt v1.3 - Adds fast & easy file encryption to Explorer My really out-of-date homepage Sonork-100.19012 Acid_Helm

            D Offline
            D Offline
            Daniel Lohmann
            wrote on last edited by
            #34

            Michael Dunn wrote: You can't make any assumptions about it actually being a pointer to the underlying data, because iterator is not documented as such. But you can expect it to support (part of) the syntactic interface of a pointer (operator *(), operator ->() and for some iterators also operator []()) Therefore if you use it in generic (templated) code you can expect it to work like a pointer. We have to be very careful with terms like "pointer", "data" and so on here. What exactly is meant by "pointer". A pointer in the C sense or a "thing" that supports the interface of a pointer and therfore acts like a pointer (which is exactly the idea of a generic iterator used in STL). I am still not sure about the intention of the original question: Given an STL iterator it, what is the correct way to access a pointer to the iterator's data? Does it mean:

            • a C-style pointer to the data the iterator refers to? Then the correct answer is c), even if I am not sure it should be written as &(*it) instead of &*it .
            • a "pointer interface" to the data the iterator refers to? Then the correct answer is a).
            • a "pointer inteface" to the iterator itself? Then the correct answer is b).
            • ...

            -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )

            M C 2 Replies Last reply
            0
            • D Daniel Lohmann

              Michael Dunn wrote: You can't make any assumptions about it actually being a pointer to the underlying data, because iterator is not documented as such. But you can expect it to support (part of) the syntactic interface of a pointer (operator *(), operator ->() and for some iterators also operator []()) Therefore if you use it in generic (templated) code you can expect it to work like a pointer. We have to be very careful with terms like "pointer", "data" and so on here. What exactly is meant by "pointer". A pointer in the C sense or a "thing" that supports the interface of a pointer and therfore acts like a pointer (which is exactly the idea of a generic iterator used in STL). I am still not sure about the intention of the original question: Given an STL iterator it, what is the correct way to access a pointer to the iterator's data? Does it mean:

              • a C-style pointer to the data the iterator refers to? Then the correct answer is c), even if I am not sure it should be written as &(*it) instead of &*it .
              • a "pointer interface" to the data the iterator refers to? Then the correct answer is a).
              • a "pointer inteface" to the iterator itself? Then the correct answer is b).
              • ...

              -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #35

              Daniel Lohmann wrote: I am still not sure about the intention of the original question: It's not that complicated. An iterator is not a pointer. "pointer" meaning "C-style pointer". iterator overloads pointer operators so you can treat it as a pointer, but that's a different story. (iterator has a ++ operator, does that make it a number?) Are we next going to debate about what the meaning of the word "is" is? ;) --Mike-- Just released - RightClick-Encrypt v1.3 - Adds fast & easy file encryption to Explorer My really out-of-date homepage Sonork-100.19012 Acid_Helm

              N D 2 Replies Last reply
              0
              • D David Stone

                Congrats to Christian Graus for thinking up the QOTD. I believe this is the first one I've seen from a CPian. Of course, I can't answer because I haven't a clue when it comes to STL. So I'll just leave the answering of it to the rest of you. :) David Stone It seemed similar to someone saying, "Would you like to meet my knife collection?" Ryan Johnston on Elaine's sig

                N Offline
                N Offline
                Nick Parker
                wrote on last edited by
                #36

                I'm sorry but where was the question, I can't seem to find it? :confused: Nick Parker


                M 1 Reply Last reply
                0
                • N Nick Parker

                  I'm sorry but where was the question, I can't seem to find it? :confused: Nick Parker


                  M Offline
                  M Offline
                  Martin Marvinski
                  wrote on last edited by
                  #37

                  Nick Parker wrote: I'm sorry but where was the question, I can't seem to find it? Scroll up. It's on the top of this page. It's called "Question of the Day". :)

                  1 Reply Last reply
                  0
                  • M Michael Dunn

                    Daniel Lohmann wrote: I am still not sure about the intention of the original question: It's not that complicated. An iterator is not a pointer. "pointer" meaning "C-style pointer". iterator overloads pointer operators so you can treat it as a pointer, but that's a different story. (iterator has a ++ operator, does that make it a number?) Are we next going to debate about what the meaning of the word "is" is? ;) --Mike-- Just released - RightClick-Encrypt v1.3 - Adds fast & easy file encryption to Explorer My really out-of-date homepage Sonork-100.19012 Acid_Helm

                    N Offline
                    N Offline
                    Nick Parker
                    wrote on last edited by
                    #38

                    Michael Dunn wrote: Are we next going to debate about what the meaning of the word "is" is? Mike, you are getting too philosophical for a Friday night. :) Nick Parker


                    1 Reply Last reply
                    0
                    • R Ryan Johnston 0

                      Chris Losinger wrote: but, i've honestly never seen that in any live code or example: it's always (*it).Foo(); Are you serious? I have never ever seen someone use (*it).Foo() instead of it->Foo(). Pointers would be so dirty without ->. Ryan Johnston

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #39

                      Absolutely. If you have a pointer to an object, within an iterator, then you need to dereference the iterator in order to get to the object. I forget what the question was ( it's not there now ), but I thought it was to do with passing a pointer, where you need to dereference the iterator, THEN grab the address of the dereferenced object, so you need to do this: &(*it) in order to turn an iterator which points to an object into a pointer to the object. Sadly, VC6 lets you use the iterator as a pointer. This is BAD, and is fixed in VC7. The fact that you can do something nonstandard in VC6 was the point of me asking the question. Having to dereference an iterator has nothing to do with pointers, iterators are not pointers, ( although pointers are iterators ), even if they act like them in some respects. As far as what Chris is talking about, I've never dereferenced an iterator to call a method, I've never had a problem on any STL version using -> to call a method on an iterator. I'd be interested to hear otherwise. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002

                      1 Reply Last reply
                      0
                      • N Nemanja Trifunovic

                        The answer is C) (*it) is the value, so &(*it) is the address of the value. Or did I miss something? I vote pro drink :beer:

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #40

                        Yes, c is the answer. The reason I asked the question is the VC6 STL is broken and accepts the iterator as a pointer to the object. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002

                        1 Reply Last reply
                        0
                        • D Daniel Lohmann

                          Michael Dunn wrote: You can't make any assumptions about it actually being a pointer to the underlying data, because iterator is not documented as such. But you can expect it to support (part of) the syntactic interface of a pointer (operator *(), operator ->() and for some iterators also operator []()) Therefore if you use it in generic (templated) code you can expect it to work like a pointer. We have to be very careful with terms like "pointer", "data" and so on here. What exactly is meant by "pointer". A pointer in the C sense or a "thing" that supports the interface of a pointer and therfore acts like a pointer (which is exactly the idea of a generic iterator used in STL). I am still not sure about the intention of the original question: Given an STL iterator it, what is the correct way to access a pointer to the iterator's data? Does it mean:

                          • a C-style pointer to the data the iterator refers to? Then the correct answer is c), even if I am not sure it should be written as &(*it) instead of &*it .
                          • a "pointer interface" to the data the iterator refers to? Then the correct answer is a).
                          • a "pointer inteface" to the iterator itself? Then the correct answer is b).
                          • ...

                          -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )

                          C Offline
                          C Offline
                          Christian Graus
                          wrote on last edited by
                          #41

                          Daniel Lohmann wrote: But you can expect it to support (part of) the syntactic interface of a pointer (operator *(), operator ->() and for some iterators also operator []()) Therefore if you use it in generic (templated) code you can expect it to work like a pointer. Try it in VC 7 and see what happens :) Daniel Lohmann wrote: a C-style pointer to the data the iterator refers to? Then the correct answer is c), even if I am not sure it should be written as &(*it) instead of &*it . Did I not put a bracket ? My bad. This is what I meant by the question though. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002

                          1 Reply Last reply
                          0
                          • M Michael Dunn

                            Daniel Lohmann wrote: I am still not sure about the intention of the original question: It's not that complicated. An iterator is not a pointer. "pointer" meaning "C-style pointer". iterator overloads pointer operators so you can treat it as a pointer, but that's a different story. (iterator has a ++ operator, does that make it a number?) Are we next going to debate about what the meaning of the word "is" is? ;) --Mike-- Just released - RightClick-Encrypt v1.3 - Adds fast & easy file encryption to Explorer My really out-of-date homepage Sonork-100.19012 Acid_Helm

                            D Offline
                            D Offline
                            Daniel Lohmann
                            wrote on last edited by
                            #42

                            Michael Dunn wrote: (iterator has a ++ operator, does that make it a number?) Operator ++ - hey, that makes it a pointer! ;P ;P :-D -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups