Tuesday, 15 May 2012

c++ - Returning a pointer from a method -


I had a certain class in my program, which I used to attract different shapes and also add shapes to events . vector & lt; XQuad * & gt; _inputMappedShape; Zero addRegularShape (container inputpoints, container output points) {XQuad Quad; SetInputPoints (tractors, some point); SetInputPoints (tractors, some point); SetArrowHandles (tractors, some point); _inputmappedShape [current index ++] = & amp; Tractor; } XQuad * getMappedShape (int index) {return_inputmap shap [index]; } Zero set input points (Exquired and quad, point * some point); Zero setopaptu point (exquired and quad, point * few points); Zero set arrowhorals (exquired and quad, point * few points);

Note that in the above code (method addRegularshape ), before I add it, by passing the quad to the reference of other methods In my main program, I was trying to retrieve the figure using the getMappedShape method, which is my purpose. An indicator will return to the quad for. XQuad * returnShape = getMappedShape (0)

I checked the memory address of the tractor allocated addRegularShape method and the address The indicator (method returned by getMappedShape ) and the memory address were the same.
However, somehow my data returned within the XQuad class when returned from getMappedShape was not true (i.e. that is not what is modified in the XQuad context in the other three Had been the member's work) and in return were some garbage values. I was not getting such a reason why this was happening and with the doubt of having corrupted or something else, I referred the reference object to addRegularShape Changed the indicator as follows.
XQuad * Quad = new Exquav (); After this, the program was starting to return the right values, but I still have some doubts why this is happening? Was not XQuad Object the right way to add archive addresses? Or there is some scope for the issue, that is, when the method goes out of the scope, the object is destroyed and from the collection that I actually return, now a garbage value has become?

You are returning the address of the local object (which will be destroyed at the end of the function call) so that You will end up with undefined behavior.

No comments:

Post a Comment