#include <iostream>
using namespace std;
#define SELECT 0
class Z
{
private:
int *z1; int *z2;
public:
Z(const int x1 = 0, const int x2 = 0);
Z(const Z &X);
int *first (void) const {return z1;};
int *second (void) const {return z2;};
~Z(void);
};
Z::Z(const int x1,const int x2){
z1 = new int(x1);
z2 = new int(x2);
}
#if SELECT == 1
Z::Z(const Z &X){
z1 = new int(*X.first() );
z2 = new int(*X.second() );
}
#else
Z::Z(const Z &X){
z1 = X.first();
z2 = X.second();
}
#endif
Z::~Z(){
delete z1;
delete z2;
}
int main()
{
Z firstZ;
Z secondZ(4,7);
Z thirdZ(secondZ);
Z *zp;
zp = new Z(3,5);
Z a(6, *(zp->first() ) ), b=a, c(0,0);
c = *zp;
cout << "Content of c: " << *c.first() << " and " << *c.second() << endl;
delete zp;
cout << "Content of c: " << *c.first() << " and " << *c.second() << endl;
return 0;
}
Hello I have this code, when I run it I get
Content of c: 3 and 5
Content of c: 14900448 and 5
however I was expecting something like
Content of c: 3 and 5
Content of c: Garbage and Garbage
However somehow c.second still points the value 5. Can you explain how this occurs?
I also wonder following: When I commented(or deleted) these lines
Z firstZ;
Z secondZ(4,7);
Z thirdZ(secondZ);
and run again I get what I expected
Content of c: 3 and 5
Content of c: 1924224 and 1907344
Can you explain how this happens?
Thanks