why in copy constructor we can use rect.p
, in main()
we can't?
class Rect
{
public:
Rect()
{
p = new int(100);
}
Rect(const Rect& r)
{
width = r.width;
height = r.height;
p = new int(100);
*p = *(r.p); // OK
}
~Rect()
{
assert(p != NULL);
delete p;
}
private:
int width;
int height;
int *p;
};
int main()
{
Rect rect1;
rect1.p = new int(200); // error
Rect rect2(rect1);
return 0;
}