Studying for the final and can't remember the details behind the copy constructor. I know what it does, just don't know the "why" behind some stuff.

Here is an example of a copy constructor I have.

Person::Person(const Person& aPerson) //m_variable=private variables
{
m_name = aPerson.get_name(); 

m_pBirthDate = new Date; 
m_pBirthDate->set_year(aPerson.get_birthYear());
m_pBirthDate->set_month(aPerson.get_birthMonth());
m_pBirthDate->set_day(aPerson.get_birthDay());
}

Why is there an "m_pBirthDate =new Date"? I get everything else is copying over the info to the new variable... is it just pointing to a new date block to create the copy?

A Copy Constructor has to completely generate a new Person object with the Person object passed as a parameter. Apparently one of the members of this Person object is a pointer to a struct/class that probably looks something like this...

class Date
{
 public:
 void set_year(short year);
 void set_month(short month);
 void set_day(short day)
 etc...
};

The newly created Person object can't use the memory allocations for the existing object passed as a parameter because if this later object gets destroyed/deleted/goes out of scope, the the new object will have bogus memory and will cause a crash. So every dynamically allocated member of the parameter class (the Person& parameter) will have to be duplicated by a memory allocation for the yet to be created object. That's what the 'new' is about. Then you'll have a completely independent object from the one passed in. So yes, you've answered your own question. It needs a new date memory block, otherwise, its sharing the other's, and won't be independent of it. Hope this helps.

When you don't specify a copy constructor the compiler does a shallow
copy. Which means that it just copies the value from one class to another.

MyClass obj ( anotherMyClassObj ); //shallow copy

Now if inside MyClass contains a pointer variable( allocated by new).
Then that copy constructor does not copy the pointers value, but
they both point at the same location. This causes many troubles.

As I was typing out my question, I realized my answer, but decided to post anyway for a clear cut explanation. Thanks for organizing my thoughts! That was very helpful and clear.

A Copy Constructor has to completely generate a new Person object with the Person object passed as a parameter. Apparently one of the members of this Person object is a pointer to a struct/class that probably looks something like this...

class Date
{
 public:
 void set_year(short year);
 void set_month(short month);
 void set_day(short day)
 etc...
};

The newly created Person object can't use the memory allocations for the existing object passed as a parameter because if this later object gets destroyed/deleted/goes out of scope, the the new object will have bogus memory and will cause a crash. So every dynamically allocated member of the parameter class (the Person& parameter) will have to be duplicated by a memory allocation for the yet to be created object. That's what the 'new' is about. Then you'll have a completely independent object from the one passed in. So yes, you've answered your own question. It needs a new date memory block, otherwise, its sharing the other's, and won't be independent of it. Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.