Storing,retrieving from an object.
-
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
-
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
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 referenceRectOne
to that temporaryRECT
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 -
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 referenceRectOne
to that temporaryRECT
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 MaunderHi 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