How do I remove double (delete) errors from shallow copied object vs original object.
A simple example:
class INT
{
int *p; //dynamic.
//define here fancy constructors etc.
set(int i){ p=new int; p=i;}
~INT();
}
INT::~INT()
{
if(p) delete p;
}
void somefunction(INT a)
{
//done some stuff ,e.g. call some display function
}
//note here, that a destructor will be called, and a.p will vanish.
int main(void)
{
INT a; a.set(2);
somefunction(a);//shallow copy
}
//CRASH BOOM BOOM!
I would like a generic solution, because passing objects is a trivial thing, and something as naive as this, resulting into a horrific/terrific error is just 'awesome'.
I suspect there are many ways around this (some of which even I can think of), but I was curious if there is any generic (applicable almost everywhere) way of solving this problem?