0

In my following function a new Object of MyClass is created and assingned to myObject. After that the destructor of the new Object is called. The original object declared in outside the function is NOT destroyed. MyClass has the dafault assignement operator.

MyClass myObject; //global varibale;

void somefuction()
{
    myObject = MyClass();
}

My Question is: Is there a way to store a new object in myObject, so that the orginal object is destroyed, without using pointers.

rkgghz
  • 456
  • 8
  • 19
  • It sounds like you are trying to get around the assignment operator not being implemented correctly. Why should `myObject` be killed? Its value is changed. – StoryTeller - Unslander Monica Mar 25 '19 at 09:20
  • Are you following [The Rule of Three](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three)? – Yksisarvinen Mar 25 '19 at 09:24
  • How are you determining that which object is destroyed? `myObject`'s address will never change – Caleth Mar 25 '19 at 09:28
  • ok so i implement the assignement operator to destroy all objects that will be overwritten? – rkgghz Mar 25 '19 at 09:29
  • Assigning is not supposed to destroy the object that's being assigned to. It should just give it a new value. If you have some operations in the destructor that you need to do in order to implement assignment, you should do those in an overloaded assignment operator as well, not destroy the object. – molbdnilo Mar 25 '19 at 09:38
  • ok thanks, I will try to do something in this direction – rkgghz Mar 25 '19 at 09:39
  • Why do you want myObject to be destroyed ? It will when it will go out of scope (if you are not using pointer). So what is its scrope ? – Gojita Mar 25 '19 at 09:40
  • I implemented the assignement operator, but that brought up a new problem: https://stackoverflow.com/questions/55335619/assignement-operator-changing-value-the-assigned-object – rkgghz Mar 25 '19 at 10:22

0 Answers0