How to remove double (delete) errors from a small copied object and the original object.
A simple example:
class INT
{
int *p;
set(int i){ p=new int; p=i;}
~INT();
}
INT::~INT()
{
if(p) delete p;
}
void somefunction(INT a)
{
}
int main(void)
{
INT a; a.set(2);
somefunction(a);
}
I would like to get a general solution, because going through objects is a trivial thing, and something as naive as this, which leads to a terrifying / terrific mistake, is just "awesome."
I suspect there are many ways around (some of which I can even come up with), but I was curious if there is any general (applicable almost everywhere) way to solve this problem?
source
share