[Gambas-user] How to clone an object?

Bruce adamnt42 at gmail.com
Fri Apr 24 09:47:50 CEST 2020


On 24/4/20 10:49 am, Benoît Minisini wrote:
> 
> You have to write the clone method for each class yourself.
Yes!
(slight translation) You have to write the clone function for a class in 
that class itself.

That way an object of that class can create an entirely new object that 
is a "clone" of that object at that particular point in time. Thus it 
can create a photocopy of itself (including it's internals) but also 
copies of any contained objects (at that point in time and as long as 
their classifiers also have Clone() functions).
Thus you do not need to know what type of classifier represented the 
object, just call it's Clone() function which will return an entirely 
new object of the same type!

But... never forget, nor assume, that the clone is the same object as 
the original. "Daisy the sheep" was an entirely new sheep, not the sheep 
it was cloned from.

b


More information about the User mailing list