C++ Trying to make a method in a class that can deep copy one object to another. Visual studio is not letting me step into the method

Viewed 23

It's supposed to allocate all memory dynamically, so I need to use the pointers. I can't troubleshoot because for some reason visual studio won't let me step into the code. I need to make a method to deep copy objects and this is the method in my class to override the = operator.

Trophy& Trophy::operator=(const Trophy& obj){

this->name = new string(*(obj.name));
this->level = new int (*(obj.level));
this->color = new Color (*(obj.color));

return *this;
}

And here's the constructor in case that helps

Trophy::Trophy() {
*name = "";
*level = 0;
*color = BRONZE; }

as well as the private part of the class

private:

string* name = new string;
int* level = new int;
Color* color = new Color;

And here's my main as simple as I could get it. The last two couts should be different if the deep copy works.

Trophy* newTrophy = new Trophy();

newTrophy->setName("My Trophy");
newTrophy->setLevel(10);
newTrophy->setColor(GOLD);

cout << newTrophy->getName() << " " << newTrophy->getLevel() << " " << newTrophy->getColor() << endl;

Trophy* copyTrophy = new Trophy();

copyTrophy = newTrophy;

cout << copyTrophy->getName() << " " << copyTrophy->getLevel() << " " << copyTrophy->getColor() << endl;

newTrophy->setName("Please work");
newTrophy->setLevel(5);
newTrophy->setColor(SILVER);

cout << newTrophy->getName() << " " << newTrophy->getLevel() << " " << newTrophy->getColor() << endl;
cout << copyTrophy->getName() << " " << copyTrophy->getLevel() << " " << copyTrophy->getColor() << endl;
0 Answers
Related