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 34 35 36 37 38 39 40 41 42 43 44 45
| #include <iostream> using namespace std;
class DeepCopy { private: int *data;
public: DeepCopy(int val) { data = new int(val); }
DeepCopy(const DeepCopy &other) { data = new int; *data = *(other.data); cout<<"deep copy"<<endl; }
int getData() const { return *data; }
void setData(int val) { *data = val; }
~DeepCopy() { delete data; } };
int main() { DeepCopy b1(5); DeepCopy b2(b1);
cout << "b1=" << b1.getData() <<" " << "b2=" << b2.getData() << endl;
b1.setData(10);
cout << "b1=" << b1.getData() <<" " << "b2=" << b2.getData() << endl;
return 0; }
|