That is because most probably your return value (by value) goes into nirvana and will be destructed ... questionable ...
This is the correct implementation of
Operator=
which will also not call the destructor:
const strin& operator = (const strin &x)
{
cout<<"= operator is called\n";
if (this != &x)
{
len=x.len;
ptr=new char[len+1];
strcpy(ptr, x.ptr);
}
return(*this);
}
Attention, possible memory leak
The above is still not quite correct. You Need also check first wheter
this
has allready allocated Memory and release it first in case. That will be your exercise
A side note, my personal preferenced way how I combine
operator=
and copy constructor
a.) Copy constructor
MyClass::MyClass (const MyClass &rhs)
: InitMember_1(),
.....
InitMember_N()
{
operator= (rhs);
}
b.) operator=
const MyClass& MyClass::operator=(const MyClass &rhs)
{
if (this != &rhs)
{
}
return(*this);
}