3

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;
  • If you use the same pointer to point to automatic and dynamic memory you'll find it's awesomely hard to tell the automatic objects that don't need manual management from the dynamic objects that you do have to manually manage. This becomes important in the `=` operator because you need to know what to do with the objects the instance being assigned is already pointing at. – user4581301 Jul 30 '22 at 04:49
  • `*name = "";` without initializing `name` smells like undefined behavior. Can you make this into a [mre]? Enough code that someone else can compile this and observe the same effects you are. – Nathan Pierson Jul 30 '22 at 04:50
  • It is very possible that you cannot step into the operator because the program broke earlier than you think. – user4581301 Jul 30 '22 at 04:52
  • Related: What if the `Trophy` behind modified in `operator=` already has memory allocations behind `name`, `level`, and `color`? Your `operator=` looks like a guaranteed memory leak. Rather than deleting and reallocating, what about `*name = *(obj.name);`, etc? Also why are any of these member variables pointers to begin with instead of values? – Nathan Pierson Jul 30 '22 at 04:52
  • Tactical note: I real life you almost never want to use `new` to get a `string`. `string`'s job is to look after the pointers and allocations for you, so manually managing the `string` defeats most of the point of using a `string`. – user4581301 Jul 30 '22 at 04:55
  • `copyTrophy = newTrophy;` assigns _the pointer_. Your `operator=` isn't invoked at any point in that line. – Nathan Pierson Jul 30 '22 at 04:56
  • I guess I assumed "dynamically allocating memory" meant everything had to be pointers. I updated the code so hopefully it's minimally reproducible. – erica_on_strike Jul 30 '22 at 04:58
  • Do what you have to do to learn *how* to use dynamic memory, but also spend some time on *when* to use dynamic memory. You'll find the *when* is [not very often](https://stackoverflow.com/q/6500313/4581301). – user4581301 Jul 30 '22 at 04:58
  • @NathanPierson Thank you so much! I guess I figured it was a full override. fun times learning new syntax – erica_on_strike Jul 30 '22 at 05:02
  • It would have been used if you'd done `*copyTrophy = *newTrophy;`, acting on the actual `Trophy` objects instead of pointers to `Trophy` objects. As you can see, it can get fairly confusing when your code uses more levels of pointer indirection than necessary. – Nathan Pierson Jul 30 '22 at 05:05

0 Answers0