Hi All,
After compiling below code , I am getting below error.
error: cannot convert a* to b* in assignment.
it seems to be assignment doesn't work with pointers . Can anybody throw some light on this?
i tried to use unique_ptr by transfering the ownership from a to b but it doesn't work.
#include <iostream>
#include <memory>
using namespace std;
class b ;
class a
{
int a1 ;
b *p;
public:
a(int x=0):a1(x){}
void fun1() {cout<<a1;}
};
class b
{
private:
a *p;
public:
b (int x=0)
{
p= new a(x);
//p1=new a(x);
}
b ( const b &obj)
{
p = new a (*obj.p);
}
const b& operator=(const a& obj)
{
if(&obj != p)
{
delete p;
p=new a(obj);
}
return *this;
}
void fun2(){p->fun1();}
~b () { delete p;}
};
int main()
{
b *obj1= new b (10);
a *obj2= new a(20);
obj1=obj2;
obj1->fun2();
/*
unique_ptr<b> obj1 ( new b (10));
unique_ptr<a> obj2 (new a(20));
obj1=move(obj2);
obj1->fun2();
*/
return 1;
}