Tuesday, July 28, 2009

C++ Why would returning an object cause it to change values?

Below is a member function which performs the intersection operation on the set. Running the debugger, everything is fine until I hit the return statement. Once executed, the return statement changes my set values from say, {1, 3, 5} to {-342325235452, -3343435353, -5555342242} or something crazy like that. What would cause return to do that?





Set Set::operator^( Set right )


{ Set temp;


for(imaSet.clear(); imaSet.isMore(); imaSet.moveNext())


{ if (right.InSet(imaSet.getCurrent()) %26amp;%26amp; !temp.InSet(imaSet.getCurrent()))


temp.imaSet.appendNode(imaSet.getCu...


}


return temp;


}

C++ Why would returning an object cause it to change values?
Without looking at the definition for class Set, it is hard to say. However, my guess is that you don't have a correct copy constructor. A copy constructor would have a signature like this:





Set::Set( const Set %26amp; s )


{ // create new set, then fill in the content with items from s


}





When you return a local variable, copy-constructor is invoked implicitly. If you don't implement copy-constructor, compiler define one for you; however, its behavior is usually not desirable.
Reply:You do mean, send something (as Set) to this operator^ and then this variable is change to itself back to its calling line.





See the code, do you mean like this.





#include %26lt;stdio.h%26gt;





struct SET


{


int a;


int b;


};





SET *get( SET *a )


{


a-%26gt;a++;


a-%26gt;b++;





return a;


};





void main()


{


SET myset;





myset.a=10;


myset.b=5;





get( %26amp;myset );





printf("%d %d",myset.a, myset.b);


}





First I keep 10 and 5 in myset. The send myset to get function by address. It increases a and b. View the result, you'll get 11 and 6 instead. This example show you how to send by address (reference) in C++. Hope it can help you.





Astalavista :)
Reply:Maybe because, let's see. temp is a stack variable that comes into scope once the operator is called. You go through the function, in which case you hit return. Return returns a value (which then gets used appropriately). But then temp goes out of scope since it is returned, and the memory for temp is no longer valid.





You're staring at crud in an invalid memory location, that's what.
Reply:temp variables scope is limited to that method only,thats why it gives junk values
Reply:The weird numbers represent the location of the values, not the actual values themselves. For instance, the 1 in the first set is located at -342325235452.





Remember that with C++ you can pass by reference (like in Java) or by value (like in C). In this case, you're passing by value because you're getting the address rather than the value. Try using %26amp;temp. You may also have to play around with pointers to get the results you need.





I know this is a bit vague, since I haven't used C++ in a while, but I hope I said enough to at least get you started in the right direction.


No comments:

Post a Comment