Type-specific methods and implementation class constructors and destructors

Type-specific methods are virtual methods. You cannot call them from the implementation class' constructors or destructors, because they will not be defined when the implementation class' constructor runs.

To perform type-specific operations at construction time, add a separate ImplementConstructor method to the implementation class. Call it from the class template's constructor, after the implementation class is constructed.

To perform type-specific operations at destruction time, add an ImplementDestructor method to the implementation called from the class template's destructor.

Here is the wrong way to implement the TOwningStackOf copy constructor:

      template <class AType>
      TOwningStackOf::TOwningStackOf(const TOwningStackOf<AType>& other)
          : TOwningStackOfImplementation(other)
      {
      }
      
      TOwningStackOfImplementation::TOwningStackOfImplementation(
          const TOwningStackOfImplementation& other)
          : fCount(other.fCount)
      {
          for ( unsigned int i = 0; i < fCount; i++ )
          {
    

    fStack[i] = TypeSpecificCopy(other.fStack[i]); } }
Here is the correct way:

      TOwningStackOf::TOwningStackOf(const TOwningStackOf<AType>& other)
      {
          ImplementConstructor(other);
      }
      
      void TOwningStackOfImplementation::ImplementConstructor(
          const TOwningStackOfImplementation& other)
      {
          fCount = other.fCount;
          for ( unsigned int i = 0; i < fCount; i++ )
          {
              fStack[i] = TypeSpecificCopy(other.fStack[i]);
          }
      }

[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