Why do you need to implement a copy constructor and assignment operator? In this answer also describe difference between shallow copy vs deep copy. code example
Example 1: deep copy c++
// DEEP COPY
class X
{
private:
int i;
int *pi;
public:
X()
: pi(new int)
{ }
X(const X& copy) // <-- copy ctor
: i(copy.i), pi(new int(*copy.pi)) // <-- note this line in particular!
{ }
};
Example 2: deep copy c++
//SHALLOW COPY
class X
{
private:
int i;
int *pi;
public:
X()
: pi(new int)
{ }
X(const X& copy) // <-- copy ctor
: i(copy.i), pi(copy.pi)
{ }
};