Next: High Integrity CPP Rule 17.8  Up: 17 Standard Template Library (STL)  Previous: High Integrity CPP Guideline 17.6  Contents

High Integrity CPP Guideline 17.7   Do not use STL containers as public base classes.

Justification

All STL containers lack a virtual destructor. If a class with a non virtual destructor is used as a base class it is possible to get undefined behaviour on destruction, this happens if the derived class is allocated on the heap and later deleted through a base class reference.

   
   class MyVector : public std::vector {};
   
   void doSomething()
   {
      MyVector* pHeapVec = new MyVector;   // allocate derived obj on heap
      std::vector* pBaseVec = pHeapVec;    // access through base class
      delete *pBaseVec;                    // undefined behaviour!
   }
See also

Rule 3.3.2


HICPP VERSION 2.4  http://www.codingstandard.com   Copyright: © 2007 THE PROGRAMMING RESEARCH GROUP