C++ Class Internal Memory Management -
i building generic classes object , array can used store data , output data various formats (json, xml, etc.). want able this:
object * o1 = new object(); object * o2 = new object(); object * o3 = new object(); o3.addnvp("obj1",o1); o3.addnvp("obj2",o2); o3.printjson();
output:
{"obj1":{},"obj2":{}}
however, since deconstructor in object calls delete on internal object pointers, i'm afraid users of class might attempt this:
object o1; object * o2 = new object(); o2->addnvp("obj1", &o1); ... delete o2; //runtime error because o2 internally attempt delete &o1!
what best way mitigate potential runtime error? documentation? deep copying instead of taking incoming references as-is? remove feature altogether , make allocate new objects object * o2 = o1.createsubobject("obj2")
? i'm not sure need worry in terms of memory management.
Comments
Post a Comment