Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo C++ Class Library Definition - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

          Container Classes


     The  classes  in  the class Object hierarchy are divided into two
     logical  sets:  those  classes  which  contain  objects  of other
     classes and those classes which do  not.  We call the first set a
     Container class.  An  Array  class,  for  instance,  maintains an
     ordering on a  collection  of  objects.  Thus an Array class is a
     derivation of a Container class.

     This chapter describes the  Container  class  and  a group of its
     derivatives,  known  collectively  as  the sequence grouping. The
     remainder of the derivatives of the Container class are discussed
     in the chapter entitled "Collection Hierarchy."


        Object Ownership


     Before  we  introduce  you  to  the  class  Container,  you  must
     understand  the  concept  of object ownership. As in real life, a
     container in C++ starts out empty and must be filled with objects
     before  the  objects  can be said to be in the container.  Unlike
     the  real  world,  however,  once  objects  are  placed into  the
     container, they are owned by the container. This is true of class
     library containers. If this  were  not the case, you could add an
     object to  a container, then destroy the object yourself, without
     the container knowing it had been destroyed. This would leave the
     container in a very confused state.

     When  the container owns an object, you  must  not  destroy  that
     object without first removing that object from the container. Or,
     you can ask  the  container  to  destroy the object for you. This
     restriction is based  on  the scoping rules and memory allocation
     in C++.

     If you declare an  automatic  object (that is, an object which is
     local  to  your  routine), and place  that  object  in  a  global
     container, your local  object  will be destroyed when the routine
     leaves the scope in  which  it was declared. To prevent this, you
     must  only add heap objects (objects that  aren't  local  to  the
     current scope)  to  containers.  Similarly,  when  you  remove an
     object  from  a  global  container,  you own that object and  are
     responsible for destroying it and freeing the space  in  which it
     resides.

See Also: Container Sequence Grouping Collection

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson