I'm a bit confused about object lifetime in C++. Let's say I have the following code. First I create the pointer MyObject (line 1). Then I create an object and point the pointer at it (line 2). Then I modify the object, and point the pointer at the resultant object (line 3). Finally, I delete the object, so that I avoid a memory leak (line 4).
MyClass * MyObject;
MyObject= new MyClass(x, y);
*MyObject= MyObject-> ModifyObject(z);
delete MyObject;
Did the original object from line 2 simply get modified in line 3? (Which means that the above code is safe). Or was a second object created in line 3, meaning that the first object from line 2 never gets deleted, creating a memory leak?
EDIT: Here's a sample of what ModifyObject(z) might look like
MyClass MyClass::ModifyObject(int z) {
int a = z;
int b = z;
return MyClass(a, b);
}