Posted by Amit Nithian on March 17, 2001 at 23:31:50:
In Reply to: Array Container posted by Leon Gunden on March 17, 2001 at 12:08:25:
: I already had the array class designed, when McQuain threw a curve ball on friday in class. I have always thought of containers as truly containing the data they hold. Whether this is logical or physical containment is inconsequential, the point is that a container should be responsible for the data it holds (hence it should both create and destroy it). The other day McQuain introduced this concept as one of association between the container and the data. Though this does make some sense now that I think about it, it introduces some implementation complexity which McQuain did not mention in class. For instance, he said that any data left in the container at the point of the container's destruction should be destroyed by the container. His argument for this was good (i.e., "there is a good change that if it doesn't there will be a memory leak"), however are we going to restrict the container class so that it cannot be passed by value? If it were passed by value the formal parameter would destroy all of the contained data when the function returns, leaving the actual parameter with a whole container full of dangling pointers. Perhaps the container should not be responsible for destroying any of its data, and this should be left as the users responsibility (but then we are giving up the benefit of the constructor/destructor sequence and we might as well be writing in C). This idea seems to introduce an extreme amount of complexity on the user of the container class since he/she must understand these issues and refrain from using pass by value, etc. Also when a multiplicity of container objects are in the same scope, each containing links to the same data, a simple remove operation becomes quite a nightmare (as each container must in turn remove the data, or risk a dangling pointer). In short, this idea makes sense when McQuain says it in class, but it seems to me to be impractical. Not to mention that it introduces some subtle encapsulation violations (how can the container guarantee a sorted sequence when the user of the container may have direct links (pointers) to the data that the container is organizing?, etc, etc). Perhaps I am missing something obvious here, but this assumption that a container is merely associated with its data seems to lead to a number of quite serious flaws (at least one of which has been ground into our heads many times as a test question, i.e. "the shallow copy problem"). Is this the correct way to model containment? (sorry to write a book here, but these questions are keeping me from completing my program design)
I was wondering parts of what he said. You have to implement the deep copy constructor to allow the user to pass the Array by value. I thought though that it would ease implemenation by making the Array own the Book/Author instances instead of making them associations because where would the deletion of these go, in the destructor of the Database class?
It confused me in general when McQuain said that it is the user's responsibility to clean up objects (namely the Data element of the Node example in class Friday).
- Amit Nithian
Post a Followup