0

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?

Johnie78
  • 27
  • 9
  • Do you have copy constructors? And why does House define that humanP member, anyway? What does it represent? – user2357112 Dec 28 '16 at 23:00
  • 3
    Perhaps try to avoid using `new`. If you put a `std::vector humans` in the `House` class then it will be copied with a default copy constructor. [Is this homework](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions)? – wally Dec 28 '16 at 23:00
  • If you post some code attempting the deep copy it will be easier to help. – wally Dec 28 '16 at 23:02
  • "so that I can copy the humanO inside the House class and then delete the object in the main file." - simply use a Human value inside the House class, not a pointer. –  Dec 28 '16 at 23:04
  • It's part of my project and one of the task is to have composition and deep copy basically. I'll make an edit in a few mins. Basically the House is the main class that works and organizes other classes. Human belongs to the House, that's why it's put in there. Okay I've seen an example with vector, let me check what I can edit – Johnie78 Dec 28 '16 at 23:05
  • I see no need to implement a deep copy for something this trivial. All you need are methods in this class, to set the values of the class members. – Sam Varshavchik Dec 28 '16 at 23:35
  • Stop using raw pointers and all these problems go away... The simplest solution is to make `House` contain `Human` – M.M Dec 29 '16 at 02:06
  • But I still need to create the Human object in main, how should I pass it and make it stay inside House class? @M.M – Johnie78 Dec 29 '16 at 02:12
  • There's a few options depending on your requirements. As mentioned above, `vector` would be a good way to represent the house being able to have zero or more humans in it – M.M Dec 29 '16 at 02:15
  • But is it worth using the vector method if I will have 1 human at all times of the programs run time? @M.M I'll still probably stick to the pointers method, I need some practice with those, but just interested – Johnie78 Dec 29 '16 at 02:18
  • If you have 1 at all times then make House have a Human member , you could use simple assignment to assign it when needed. – M.M Dec 29 '16 at 02:37

1 Answers1

2

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);
Mikel F
  • 3,567
  • 1
  • 21
  • 33
  • So basically I would have to write additional method in the House class which gets a Human type pointer and inside that method I have created another Human type pointer and just make pointer1 = pointer2? Can I do the same with constructor? Can I delete newPerson after I pass it on? – Johnie78 Dec 28 '16 at 23:22
  • 1
    @Johnie78 pointer1=pointer2 will be a shallow copy. I have added more to my answer for dealing with the need for a deep copy. – Mikel F Dec 28 '16 at 23:31
  • Thank you! it's way more clear now. Yeah I see now that that would be a shallow copy, both would point to the same thing, couldn't delete the one in the main – Johnie78 Dec 28 '16 at 23:36
  • Another question, my compiler doesn't support make_unique thingy and I found that you can write all that using template like here [link](http://stackoverflow.com/questions/24609271/errormake-unique-is-not-a-member-of-std) Could you rewrite it using this? @Mikel – Johnie78 Dec 29 '16 at 01:17
  • 1
    @Johnie78 Added an update to create the unique_ptr via constructor. – Mikel F Dec 29 '16 at 01:17
  • One more question @Mikel sorry to bother you. Edit in the main post – Johnie78 Dec 29 '16 at 01:57