• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Easy way to copy class instance?

Phil7

Moderator
CX Code Contributor
3rd Party Tool Dev
Joined
Jun 26, 2017
Messages
886
Is there an easy way to copy an instance? Make a clone object?
Until now I just copied all the Fields to a new object of that class, but this time my class has some collections/arrays in it, I wanted to know if there is an other way.

BTW: Am I correct with this: "=" assigns only values (copies) for int, float, string types and it assigns something pointer like to arrays, objects (incl. maps, stacks, lists)?
 
Am I correct with this: "=" assigns only values (copies) for int, float, string types and it assigns something pointer like to arrays, objects (incl. maps, stacks, lists)?
I think.... Yes, other than basic type, Cerberus use reference to a memory address where the object is really stored. When no more variable refer to it the GC will free that memory. String also object but I thing every assignment for a string, a compiler create a duplicate of it.
 
There is no easy way to automatically copy an object, because you might want to do it in different ways. For example, you might be happy to just copy pointers to the same object fields (this is known as a shallow copy) or you might want to create entirely new instances of the object fields (a deep copy).

If you copied a sprite, it might have an image field where you would want a shallow copy, and a position field where you would want a deep copy - so you would have an object and its identical twin which start in the same place but can move separately.

Of course if your field objects have their own cloning methods already, or you have functions to copy arrays etc., it gets easier. If the position has a copy constructor, your sprite code might look like:

Cerberus:
Class Sprite
    
    Field img:Image
    
    Field pos:Position

    Method New( other:Sprite )
        img = other.img
        pos = New Position( other.pos )
    End
    
End


Class Position

    Field x:Int
    Field y:Int
    
    Method New( other:Position )
        x = other.x
        y = other.y
    End

End
 
Thanks! In my case it would be quite handy to have a way to do a complete deep copy. But if there was one I would have definitely not been aware of the consequences like having all the recource data like images twice.
So deep copy should be avoided if possible, because it can lead to a lot of operations that might not be necessary.

One idea that came to my mind, dealing with this, was that it would be really nice to enhance the array functionality (which can easily be transfered to lists, maps, etc.:
1. Copy() Method for a simple deep copy
2. InsertArray(InsArray[], Position:Int=-1) Method to insert or append an Array to an exisisting one.
 
Back
Top Bottom