Thanks for the response. Somewhere on stackoverflow someone said, "There is a rule in C++, for every new there is a delete." Is this always true? Why isn't the y pointer deleted if it's contained in object x?
There is a rule in C++, for every new there is a delete." Is this always true?
Yup, that's true.
Why isn't the y pointer deleted if it's contained in object x?
How does the compiler know if y has already been deleted or not? y is simply a variable containing an address. Compiler doesn't know if that address is from the heap, or points to something else. X may not "own" what y points to.
"There is a rule in C++, for every new there is a delete." Is this always true?
Yeah, that's pretty much it, but you have to write the delete (don't forget writing delete!), it does not just magically appear somewhere.
Why isn't the y pointer deleted if it's contained in object x?
Because you never call delete on y, it's as simple as that.
Memory deallocation is the responsibility of the developer.
If you don't want to think about it (which is usually the case) you should use std::shared_ptr or std::unique_ptr