How to make a deep copy?

1.8k views Asked by At

So basically let's say I have 2 classes now. 1 is called Human and the other one is called House.

What I have done now is made that the house makes and destroys human, so basically in the House .h file I have

    Human *humanP;

And in the .cpp files constructor

    humanP = new Human;
    humanP->something(); // lets me access the methods in the Human class

As far as I know this makes a composition and House creates/destroys the Human object. But I need to add parameters to my Human object like height and age for example.

In the main I want to have something like

int age, height;

cout << "Whats your age? << endl;
cin >> age;
cout << "Whats your height? << endl;
cin >> height;

With this I want to make

Human humanO(age, height);

Which will create Human object with those parameters. But I still want the Human object to be held in House class and then destroyed there. As far as I know I need to make a deep copy of that, so that I can copy the humanO inside the House class and then delete the object in the main file.

I've been looking trough examples but there are quite a lot of different ones, could anyone write what the code would look like to make a deep copy of this Human object that is created in main?

Edit:

Making an edit here instead of replying cause it's easier to write code here.

Okay another stupid question. If I use the simple method with

Human *newPerson = new Human

and do

House house;
house.addHuman(newPerson)

while having the class method

addHuman(Human *other)
{
   this->humanP = other;
   cout << humanP->getAge() << endl << endl << endl;
}

it works fine and gives me the age.

If I use smart pointers it doesn't work, what should I change? It gives me errors like "no matching function". What arguments should I put into the addHuman() so that it would take the smart pointer thingy?

1

There are 1 answers

6
Mikel F On BEST ANSWER

A deep copy merely means that you have allocated space for the second copy and duplicated the contents of the original into that space, as opposed to a shallow copy, which is effectively a 'pointer' to the original object that becomes invalid after the original is destroyed.

You only need a deep copy if you have to have more than one owner of the data. If your House object is meant to own the data, creating a Human' dynamically and then passing it to theHouse` instance will do the trick.

Human *newPerson = new Human(age,height);

house->AddHuman(newPerson);

Or, if you want to take advantage of smart pointers:

std::unique_ptr<Person> newPerson = std::make_unique<Person>(age,height);

or

std::unique_ptr<Person> newPerson(new Person(age,height));

and then

house->AddHuman(std::move(newPerson));

If your project absolutely needs to perform a deep copy, then you probably don't want to allocate locally.

house->AddHuman(Person(age,height));

Where House has a new method, AddHuman() that looks something like this:

void House:AddHuman(Person& newHuman);

If House stores Human objects in a vector, and Human objects can be copied trivially:

m_Humans.PushBack(newHuman);