I have a "twisted" question... Suppose to have a class like
class MyClass {
public:
 MyClass();
 ~MyClass();
MyClass& operator=(const MyClass& obj); 
private:
  int* mem;
};
where basically MyClass inits somehow mem (with a new call), the ~MyClass() deallocates mem with the delete operator.
Suppose moreover the operator = is overloaded with the code
MyClass& operator=(const MyClass& obj) {
 if(this != &obj) {
   //allocate memory of "this" with the "new" operator and copy "obj" data;
 }
return *this;
}
my question is basically the following... With the following sequence of statements
//statement 1 for short
MyClass my_obj = some_obj_of_MyClass;
i guess everything is fine since the operator = allocates memory and copies the data, but with the following
//statement 2 for short
MyClass obj; //allocate memory for "mem" 
obj = some_obj_of_MyClass;
if think it is not correct the implementation i proposed since i don't delete memory allocated early. I could call the destructor inside the assignment operator block, but in that case probably the statement 1 wouldn't be safe.
So what it is a safe way to implement everything? I guess the problem here can be to understand when the destructor is called or how to implement it properly.
 
    