Font size
Site color
Preskočiť na hlavný obsah

Copy Constructor

The third type of constructor you will learn about is the copy constructor. It is a special type of constructor used to create a new object that represents a copy of an existing object. A copy constructor is not predefined, which means you must explicitly define it inside your classes.

Suppose the task is to create a Person class to work with person data such as first and last name and year of birth. The class should enable creating an object whose fields are initialized using a parameterized constructor, as well as creating copies of an existing object.

Creating an object whose fields are initialized using a parameterized constructor was covered in the previous lesson:

class Person
{
    public string fullName;
    public int birthYear;

    public Person(string name, int year)
    {
        fullName = name;
        birthYear = year;
    }
}

Creating copies of an existing object is possible using a copy constructor. A copy constructor has one parameter — an object of the same class type as the object you want to copy. Calling this constructor copies all relevant data from the existing object into the new object. You can define and use a copy constructor, for example, like this:

using System;

class Person
{
    public string fullName;
    public int birthYear;

    public Person(string name, int year)
    {
        fullName = name;
        birthYear = year;
    }

    public Person(Person personToCopy) // Copy constructor
    {
        fullName = personToCopy.fullName;
        birthYear = personToCopy.birthYear;
    }
}

class Program
{
    static void Main()
    {
        Person p1 = new Person("Velimir Radlovacki", 1976);
        Person p2 = new Person(p1);
        Console.WriteLine(p1.fullName + ", " + p1.birthYear);
        Console.WriteLine(p2.fullName + ", " + p2.birthYear);
    }
}

Executing this program instantiates object p1 of class Person, whose fields are initialized by the parameterized constructor. After that, object p2 of class Person is instantiated, whose fields are initialized by the copy constructor (field values are copied from object p1).

In the example above, the copy constructor is said to create a shallow copy of object p1. A shallow copy involves copying field values (primitive types and references). If the Person class contained references to other objects, modifications to those objects would affect both object p1 and object p2.

If the class contains references to other objects, you need to create a deep copy. For example, if the Person class also contains an Address field of type Address:

using System;

class Address
{
    public string streetAndNumber;
    public string cityAndZipCode;

    public Address(string street, string cityZip)
    {
        streetAndNumber = street;
        cityAndZipCode = cityZip;
    }

    public Address(Address addressToCopy)
    {
        streetAndNumber = addressToCopy.streetAndNumber;
        cityAndZipCode = addressToCopy.cityAndZipCode;
    }
}

class Person
{
    public string fullName;
    public int birthYear;
    public Address Address;

    public Person(string name, int year, Address addr)
    {
        fullName = name;
        birthYear = year;
        Address = addr;
    }

    public Person(Person personToCopy)
    {
        fullName = personToCopy.fullName;
        birthYear = personToCopy.birthYear;
        Address = new Address(personToCopy.Address);
    }
}

class Program
{
    static void Main()
    {
        Address a1 = new Address("Sterijina 40", "Vrsac 26300");
        Person p1 = new Person("Velimir Radlovacki", 1976, a1);
        Person p2 = new Person(p1);
        Console.WriteLine(p1.fullName + ", " + p1.birthYear +
            ", " + p1.Address.streetAndNumber + ", " + p1.Address.cityAndZipCode);
        Console.WriteLine(p2.fullName + ", " + p2.birthYear +
            ", " + p2.Address.streetAndNumber + ", " + p2.Address.cityAndZipCode);
    }
}

In this case, the copy constructor of class Person creates a new copy of the Address object, thereby ensuring a deep copy of object p1.