The example class: an owning stack

The remainder of this guide uses an owning stack to illustrate the various template techniques. The example uses a stack because it is simple to implement. The owning feature forces the examples to deal with type-specific copy and delete operations. To make the examples short, there is no error checking.

Here is the owning stack as an ordinary, nonspecialized class:

      // Copyright (C)1994 Taligent, Inc. All rights reserved.
      // $Revision: $
      #ifndef Taligent_EXAMPLE1
      #define Taligent_EXAMPLE1
      
      class TCollectibleLong;
      
      class TOwningStackOf1
      {
      public:
                  TOwningStackOf1();
                  TOwningStackOf1( const TOwningStackOf1& other );
          virtual ~TOwningStackOf1();
                  // Operator= omitted. It's like the copy constructor.
      
          virtual void Adopt( TCollectibleLong* item );
                  // Orphan omitted. It's like Adopt.
      
          virtual unsigned int    Count() const;
      private:
          TCollectibleLong*       fStack[10];
          unsigned int            fount;
      };
      
      #endif

      // Copyright (C)1994 Taligent, Inc. All rights reserved.
      // $Revision: $
      #ifndef Taligent_EXAMPLE1
      #include <Example1.h>
      #endif
      
      #ifndef Taligent_CLASSICDATASTRUCTURES
      #include <ClassicDataStructures.h>
      #endif
      
      TOwningStackOf1::TOwningStackOf1()
          : fCount(0)
      {
      }
      
      TOwningStackOf1::TOwningStackOf1(
          const TOwningStackOf1& other)
          : fCount(other.fCount)
      {
          for ( unsigned int i = 0; i < fCount; i++ )
          {
              fStack[i] = new TCollectibleLong(*other.fStack[i]);
          }
      }
      
      TOwningStackOf1::~TOwningStackOf1()
      {
          for ( unsigned int i = 0; i < fCount; i++ )
          {
              delete fStack[i];
          }
      }
      
      void TOwningStackOf1::Adopt( TCollectibleLong* item )
      {
          fStack[fCount++] = item;
      }
      
      unsigned int TOwningStackOf1::Count() const
      {
          return fCount;
      }
To templatize the class without sharing the implementation:

      // Copyright (C)1994 Taligent, Inc. All rights reserved.
      // $Revision: $
      #ifndef Taligent_EXAMPLE2
      #define Taligent_EXAMPLE2
      
      #ifndef Taligent_PRIMITIVECLASSES
      #include <PrimitiveClasses.h>
      #endif
      
      template <class AType>
      class TOwningStackOf2
      {
      public:
                  TOwningStackOf2();
                  TOwningStackOf2( const TOwningStackOf2<AType>& other );
          virtual ~TOwningStackOf2();
          virtual void Adopt( AType* item );
          virtual unsigned int Count() const;
      private:
          AType*  fStack[10];
          unsigned int    fCount;
      };
      
      #ifndef Taligent_EXAMPLE2TEMPLATEIMPLEMENTATION
      #include <Example2TemplateImplementation.h>
      #endif
      
      #endif

      #ifndef Taligent_EXAMPLE2TEMPLATEIMPLEMENTATION
      #define Taligent_EXAMPLE2TEMPLATEIMPLEMENTATION
      
      #ifndef Taligent_EXAMPLE2
      #include <Example2.h>
      #endif
      
      template<class AType>
      TOwningStackOf2<AType>::TOwningStackOf2()
          : fCount(0)
      {
      }
      
      template<class AType>
      TOwningStackOf2<AType>::TOwningStackOf2( const TOwningStackOf2<AType>& other )
          : fCount(other.fCount)
      {
          for ( unsigned int i = 0; i < fCount; i++ )
          {
              fStack[i] = new AType(*other.fStack[i]);
          }
      }
      
      template<class AType>
      TOwningStackOf2<AType>::~TOwningStackOf2()
      {
          for ( unsigned int i = 0; i < fCount; i++ )
          {
              delete fStack[i];
          }
      }
      
      template<class AType>
      void TOwningStackOf2<AType>::Adopt( AType* item )
      {
          fStack[fCount++] = item;
      }
      
      template<class AType>
      unsigned int TOwningStackOf2<AType>::Count() const
      {
          return fCount;
      }
      
      #endif

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker