Hi,
While executing below code i am getting error like "memory clobbered before allocated block" and due to this assignment is not happening. could you let me know what can be reason for the same. I do understand it is not good to use raw pointer but in current scenario i don't have other than any option.
#include<iostream.h>
using namespace std;
class Derived1
{
int *m_p;
public:
Derived1 (int *m_p):m_p(m_p){};
Derived1& operator =( const Derived1 &obj);
void show()const {cout<<"integer address="<<(void*)m_p<<endl;
cout<<"integer="<<*m_p<<endl;
}
~Derived1(){
delete m_p;
}
};
Derived1& Derived1::operator=(const Derived1 &obj)
{
if (this!=&obj)
{
cout<<"We are Here";
int *orig =m_p;
m_p=new int (*obj.m_p);
delete orig;
}
cout<<"objects are same";
return *this;
}
int main()
{
int x=110;
int y=20;
Derived1 s1(&x) ;
s1.show();
Derived1 s2(&y);
s2.show();
s2=s1;
s1.show();
s2.show();
}