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. General Programming
  3. ATL / WTL / STL
  4. Storing,retrieving from an object.

Storing,retrieving from an object.

Scheduled Pinned Locked Moved ATL / WTL / STL
dockerquestion
3 Posts 3 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.
  • U Offline
    U Offline
    User 616616
    wrote on last edited by
    #1

    I am confused and why I can not store information in an object. If I want to save this information what I have to do? #include std::list MyList; RECT rc = {0,0,0,0}; MyList.push_back(rc); std::list::iterator it = MyList.begin(); RECT& RectOne = (RECT) *it; RectOne.left = -1; RECT& RectTwo = (RECT) *it; int left = RectTwo.left; RectOne is reference to first element of the RECT container. I changed the value of the first element of the container, and look the value in RectTwo, but it didn’t change the value. Real confused. :(( Agha Khan

    M 1 Reply Last reply
    0
    • U User 616616

      I am confused and why I can not store information in an object. If I want to save this information what I have to do? #include std::list MyList; RECT rc = {0,0,0,0}; MyList.push_back(rc); std::list::iterator it = MyList.begin(); RECT& RectOne = (RECT) *it; RectOne.left = -1; RECT& RectTwo = (RECT) *it; int left = RectTwo.left; RectOne is reference to first element of the RECT container. I changed the value of the first element of the container, and look the value in RectTwo, but it didn’t change the value. Real confused. :(( Agha Khan

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      What appears to be happening is that due to the cast

      RECT& RectOne = (RECT) *it;

      the compiler is generating a temporary object which is a copy of the object referred to by the return value of *it, then binding the reference RectOne to that temporary RECT object. Similarly, RectTwo is bound to a different temporary object. The answer is simple: remove the casts. The compiler no longer generates temporaries, and both references are bound to the original object. Stability. What an interesting concept. -- Chris Maunder

      F 1 Reply Last reply
      0
      • M Mike Dimmick

        What appears to be happening is that due to the cast

        RECT& RectOne = (RECT) *it;

        the compiler is generating a temporary object which is a copy of the object referred to by the return value of *it, then binding the reference RectOne to that temporary RECT object. Similarly, RectTwo is bound to a different temporary object. The answer is simple: remove the casts. The compiler no longer generates temporaries, and both references are bound to the original object. Stability. What an interesting concept. -- Chris Maunder

        F Offline
        F Offline
        f64
        wrote on last edited by
        #3

        Hi Agha, Mike is absolutely right and I can tell you why, you are doing the wrong cast. You have a reference to a RECT and you are casting to a RECT. This is what you should do, if you want to keep the cast RECT& RectOne = (RECT**&**) *it; RectOne.left = -1; RECT& RectTwo = (RECT**&**) *it; int left = RectTwo.left; Regards, Fabian

        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