Please help me fix my deep copy example
Nov 17, 2018 at 4:26pm UTC
I'm trying to memorize and type out a deep copy class, what is wrong with line 9 "error request for member obj in ((dcopy)*this)->dcopy::x which is of non class type *int"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include<iostream>
class dcopy
{
public :
dcopy(int m)
{*x = new int ; *x=m;}
dcopy(const dcopy & obj)
{x=new int ; *x.obj=getX();}
~dcopy()
{delete x;}
void printX()
{std::cout<<"x = " << x <<std::endl;}
int getX()const {return *x;}
void setX(int m) {*x=m;}
private :
int *x;
};
int main()
{
dcopy obj1(10);
dcopy obj2();
obj2=obj1;
std::cout <<"Both objects after copy " <<std::endl;
obj2.setX(12);
std::cout<<"Both objects after change " <<std::endl;
obj1.printX();
obj2.printX();
}
obj1.printX();
obj2.printX();
Last edited on Nov 17, 2018 at 4:45pm UTC
Nov 17, 2018 at 4:46pm UTC
You're using the dot operator on 'x', which is type int*. Did you mean *x = obj.getX();?
Nov 17, 2018 at 4:54pm UTC
thanks, silly typo :-)
Topic archived. No new replies allowed.