| 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 |