B
Brian Buderman
Consider the creation of a class library, where the desire is to allow
the user of the library to chain objects together during construction in
a single statement. For example, this statement uses the heap:
topLevelObject = new type1(new type2(new type3(), new type4());
While this example only uses 4 types, in actuality there will object
trees with a much higher count. Some of the types accept abstract
interfaces, and typeinfo must be maintained such that the library can
correctly traverse the object tree. After construction of these object
trees, the user will pass them to another library object, and be done
with it.
Is it best to:
a. Use pointers, and delete pointers automatically in each class
destructor (freeing user from deleting them but preventing them from
keeping a reference)
b. Use pointers, and require user to delete pointers manually (thus
killing the ability to chain the construction since a pointer to each
object now needs to be kept)
c. Use references - though I'm not sure how that would work with
abstract types...how would you pass an inline contructed derived class
reference where the abstract class reference is expected?
My inclination is to use choice a, though I know pointers are not type
safe, and I'm not sure how good of an idea it is to automatically delete
all the objects in the tree.
Thoughts?
Thanks in advance.
the user of the library to chain objects together during construction in
a single statement. For example, this statement uses the heap:
topLevelObject = new type1(new type2(new type3(), new type4());
While this example only uses 4 types, in actuality there will object
trees with a much higher count. Some of the types accept abstract
interfaces, and typeinfo must be maintained such that the library can
correctly traverse the object tree. After construction of these object
trees, the user will pass them to another library object, and be done
with it.
Is it best to:
a. Use pointers, and delete pointers automatically in each class
destructor (freeing user from deleting them but preventing them from
keeping a reference)
b. Use pointers, and require user to delete pointers manually (thus
killing the ability to chain the construction since a pointer to each
object now needs to be kept)
c. Use references - though I'm not sure how that would work with
abstract types...how would you pass an inline contructed derived class
reference where the abstract class reference is expected?
My inclination is to use choice a, though I know pointers are not type
safe, and I'm not sure how good of an idea it is to automatically delete
all the objects in the tree.
Thoughts?
Thanks in advance.