class table{
name *p; int i;
public:
table(int j=15){p=new name[i=j]} //constructor
~table(){delete[]p;}
}
void h()
{
table t1;
table t2=t1;
table t3;
t3=t2;
}`
Bjarne wrote array created for t1 will be deleted 3 times as array created for t1 appears in t1,t2,t3**
Can you please explain what does this mean with some other examples.
If the copy constructor and operator = only copy the value of a pointer, in the end t1, t2 and t3 will have pointers to the same bit of memory. If table has a destructor that deletes the pointer, the same pointer will be deleted three times – once for every object. This should cause a segfault.
To prevent this a class can allocate a new array for every copy (called making a deep copy), or do reference counting.