About copying a class. Value vs Address

Hi,everyone.

I have a class which contain some value like names, hp.

And I want to “duplicate” an object of a same class, that only copy all the Value inside this obj to another obj.
I code in this way.

(Example of copying ObjA to ObjB)

public Class ObjA;
public Class ObjB;
ObjB = ObjA;

But what this statement do is just put the address of classA to classB. That’s mean they point to the same obj class!

It is possible that “Em3regency” said copy every single value like this, it is good, but are there any shortcuts to do that?

anObject.name = secondObject.name;
anObject.SomeOtherVariable = secondObject.SomeOtherVariable;

Hope to get answer in C#.

Thanks,

Harzi

I personally like do this with the ICloneable interface. Something like this:

public class Person : ICloneable
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(string name, int age)
    {
        this.Name = name;
        this.Age = age;
    }

    public object Clone()
    {
        Person clone = new Person(this.Name, this.Age);
        return clone;
    }
}

Then you can just do the following thing.

Person person1 = new Person("Name", 22);
Person person2 = (Person) person1.Clone(); // Deep copy of person 1

You have two approaches for that either you create a deep copy method:

public class MyClass:MonoBehaviour{
   private int hp;
   public void Copy(MyClass original){ // You could implement the IClonable interface
      this.name = original.name;
      this.hp = hp;
   }
}

and you call it:

MyClass a;
MyClass b;

b.Copy(a); // b receives values from a

Note that even though the hp is private, the b object is able to see the private members from the a object. This is because C# like C++ uses class encapsulation so the member is private to the class but two objects of the same class can see each other private members.

Since you cannot overload the = sign in C#, you cannot do what you were after, only the reference is copied and you then have two references pointing to the same object.

Now, you can use structs instead which in turn copy all info into the other one

public class MyClass:MonoBehaviour{
    public Info info;
    public int hp;
    void Awake(){
	info.hp = hp;
	info.name = name;
    }
}
public struct Info{
    public string name;
    public int hp;
} 

public class Manager:MonoBehaviour{
    public MyClass a;
    public MyClass b;
    public void Start(){
       a.info = b.info; // All values from info in b are now copied to info in a
    }
}

The public variables can be used to set the original values and then you set one to receive the values from the other. This is just for you to see but I would use the first version.

This is because classes are reference type while structs are value type so when assigning one to the other, in class case, you assign the address while in struct case you assign all values.

If you are coding the class, just add a copy constructor. It allows you to do things like:

public ClassA objectA = new ClassA();
// Fill objectA
public ClassA objectB = new ClassA(objectA);