I want to build a copy constructor Pair(const Pair& other)
. This takes as its argument a read-only reference to another Pair
. It should set up the newly constructed Pair
as a "deep copy". But I have no idea on how to set up the integers at these new locations, which should be assigned values according to the integers that the other Pair
is pointing to.
class Pair {
public:
int *pa,*pb;
Pair(int a, int b);
Pair(const Pair & other);
~Pair();
};
Pair::Pair(int a, int b){
pa = new int;
pb = new int;
*pa = a;
*pb = b;
}
Pair::Pair(const Pair & other){
pa = new int;
pb = new int;
*pa = *(other.pa);
*pb = *(other.pb);
}
Pair::~Pair(){
delete pa;
delete pb;
}
int main() {
Pair p(15,16);
Pair q(p);
Pair *hp = new Pair(23,42);
delete hp;
std::cout << "If this message is printed,"
<< " at least the program hasn't crashed yet!\n"
<< "But you may want to print other diagnostic messages too." << std::endl;
return 0;
}