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 after
the statement of return, the local & temporary object tempMoney will be
destroyed.
Now, if in JAVA, 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 refering to is
referenced by other variable. So it stays existing. What happens to the
local & temporary variable tempMoney? Will it be destroyed after the
function has finished?
Another question:
In C++,
Money aMoney, bMoney;
...
bMoney = aMoney;
Now bMoney and aMoney are referring to different but identical objects.
But in JAVA,
bMoney = aMoney;
will make bMoney and aMoney refer to the SAME object. Is this correct?
Can I make assignment("=") in JAVA behaves like in C++?
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 after
the statement of return, the local & temporary object tempMoney will be
destroyed.
Now, if in JAVA, 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 refering to is
referenced by other variable. So it stays existing. What happens to the
local & temporary variable tempMoney? Will it be destroyed after the
function has finished?
Another question:
In C++,
Money aMoney, bMoney;
...
bMoney = aMoney;
Now bMoney and aMoney are referring to different but identical objects.
But in JAVA,
bMoney = aMoney;
will make bMoney and aMoney refer to the SAME object. Is this correct?
Can I make assignment("=") in JAVA behaves like in C++?
Thank you very much.