X
Xiaoshen Li
Dear Sir,
I am a little puzzled about a function returning a class object, for
example, suppose I hava a class Money and a method:
Money lastYear(Money aMoney)
{
Money tempMoney;
...
return tempMoney;
}
Because in C++ the RETURNED is actually a new copy of the object(kind of
like passing by value), here a copy of tempMoney is returned, so
tempMoney will be useless, so after the statement of return, the object
tempMoney will be destroyed. Is this correct?
Now, if in JAVA(sorry, not offending), since always passing by
reference, if above code is Java, the RETURNED is the MEMORY ADDRESS of
the object tempMoney. So after the exit of the function, the object
tempMoney is referenced by other variable. So it stays existing. Is this
correct?
Thank you very much.
I am a little puzzled about a function returning a class object, for
example, suppose I hava a class Money and a method:
Money lastYear(Money aMoney)
{
Money tempMoney;
...
return tempMoney;
}
Because in C++ the RETURNED is actually a new copy of the object(kind of
like passing by value), here a copy of tempMoney is returned, so
tempMoney will be useless, so after the statement of return, the object
tempMoney will be destroyed. Is this correct?
Now, if in JAVA(sorry, not offending), since always passing by
reference, if above code is Java, the RETURNED is the MEMORY ADDRESS of
the object tempMoney. So after the exit of the function, the object
tempMoney is referenced by other variable. So it stays existing. Is this
correct?
Thank you very much.