78.5k views
5 votes
If object1 and object2 are objects of the same class, to make object2 a copy of object1:

2 Answers

7 votes
... you could overload and implement your assignment operator. They have already been instantiated, so applying a copy constructor is too late I suppose.
User Oron Bendavid
by
7.4k points
2 votes

Answer:

Any of 2 methods can be used a Deep copy or a Shallow copy

Step-by-step explanation:

There are 3 ways of copying an object of a class :

1) Shallow copy - This makes copies of all the primitive data type variables but it creates reference to the pointer variable. It means kit doesn't copy them it just creates a new reference to that same address.The default copy constructor and assignment operator make shallow copies.

2) Deep copy - A deep copy copies all member variables, and makes copies of dynamically allocated memory pointed to by the variables. To make a deep copy, we have to define our copy constructor and overload the assignment operator.

Requirements for deep copy in the class :

• A destructor is required to delete the dynamically allocated memory.

• A copy constructor is required to make a copy of the dynamically allocated memory.

• An overloaded assignment operator is required to make a copy of the dynamically allocated memory.

User Martyn Lovell
by
7.9k points

Related questions