#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 *zp;
zp = new Z(3,5);
Z 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;
}
Hello, when I run this code, I get something like
Content of c: 3 and 5
Content of c: Garbage and Garbage
I was expecting this since, I am not creating another memory for c instead it points contents of zp. However, when I switch #define SELECT 1, now I am creating new memory for c. So when I delete zp, c still should point correct values (stored in different memory than zp) but what I get is still garbage shown below.
Content of c: 3 and 5
Content of c: Garbage and Garbage
Where is the problem?
I have another question. When I debug this in VScode, I get "Exception Occured. Unknown signal "for the lines
delete z1;
delete z2;
In CodeBlocks, there is no error. What is the problem?
Thanks for help.