Person
, how do I create a copy of Alice's pet and assign it to the copied Person's pet reference? copiedPet
). What I'd like the copy-constructor to do is create another Pet
object that is independent of Alice's pet.pet
looks like it should be a Pet*
rather than a Pet&
. Thoughts on this?
|
|
delete
`copiedPet'?¿why does `Person' hold a reference to `Pet' instead of an object? |
Pet pet;
member, it'd be a copy of a Pet object and not one that was already created. ¿what's the point of lines 56--57? ¿where do you delete `copiedPet'? |
pet
in the Person's class destructor. ¿what's the relationship that you want to represent between `Pet' and `Person'? |
|
|
1a. Sherlock.age: 3 1b. Sherlock.name: Sherlock 2. Sherlock.age: 3 3a. Alice.Sherlock.age: 4 3b. Sherlock.age: 4 4a. CopyAlice.Sherlock.age: 4 4b. Sherlock.age: 4 5a. Sherlock.age: 6 5b. Sherlock name: Sherlock 5c. Sherlock name: Sherlock 6. CopySherlock.age: 5 7. CopySherlock name: Copy_of_Sherlock |
If Person had a Pet pet; member, it'd be a copy of a Pet object and not one that was already created. |
it doesn't make sense for a Pet object to be destroyed when a Person is destroyed |
I don't know how to use the copy-constructor to allocate stack memory. |
The main difference is the age and aging of the Pet are a property and method of the Pet and not the Person. Ask yourself the question when you encapsulate each of the two x=classes "How can a Person age a Pet?" |
class Person { public: std::string name; Pet* pet; Person(std::string name, Pet* pet) : name(name), pet(pet){} Person(const Person& other) : name(other.name), pet(other.pet){} ~Person(){} }; |
Pet
object. Then when should a Pet object be destroyed? If you create a Person on the heap, how will you guarantee that the Pet object isn't destroyed before the Person? ... If you really want people and pets to be loosely coupled then you need a collection that owns the pets (meaning the collection destroys them) and a person contains an iterator into the pet collection, or a pet ID that can be used to lookup the pet. |
It can't. Objects can be constructed in global memory, on the stack, on the heap, or anywhere else really. Why would objects in all of those places what to allocate memory on the stack? |
|
|
pet
) to be heap allocated because then you'd have to explicitly deallocate it. copiedPerson
has local scope. You'd want all the memory tied to that object to be on the stack so that when the function ends, they too can automatically be deallocated.
Person(const Person& other) : name(other.name), pet(other.pet)
you say `this->pet' and `other.pet' refer to the same objectpet = *copiedPet;
you are modifying the state of the object (i.e., the value of its member variables)this
lifetime
|
|
copiedPerson has local scope. You'd want all the memory tied to that object to be on the stack so that when the function ends, they too can automatically be deallocated. |
|
|
What I'd like the copy-constructor to do is create another Pet object that is independent of Alice's pet. |
doesn't compile, conversion from `int' to `Person*' also, stack overflow no idea what you wanted to show |
|
|