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?

Dani AI

Generated

A short, practical addendum to the helpful replies from , and .

If a class "owns" a heap object via a raw pointer, the copy constructor must create a separate heap allocation so the two objects do not share the same memory. In modern C++ there are three common, safer approaches to handle this:

  • Prefer value semantics: make Date a plain member (for example Date m_birthDate;). If Date is small and copyable, the compiler-generated copy will do the right thing and you avoid manual memory management.
  • Use smart pointers: std::unique_ptr<Date> expresses exclusive ownership; std::shared_ptr<Date> allows shared ownership. Smart pointers remove most manual memory pitfalls, though they change copying semantics (unique_ptr is not copyable).
  • If you must use raw owning pointers, follow the Rule of Three (or Rule of Five in C++11+): implement a destructor, copy constructor, and copy-assignment operator (and move operations where appropriate). For assignment, consider the copy-and-swap idiom to get strong exception safety.

Practical tips: always free owned memory in the destructor, allocate before you change the current object when implementing assignment, and prefer RAII (resources tied to object lifetime). For formal details see the C++ reference on copy constructors and the Rule of Three: C++ copy constructor and Rule of Three.

Recommended Answers

All 3 Replies

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.