Inheriting from std::vector

This topic is 3847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Inheriting from std::vector is evil because it hasn't got a virtual destructor. However, what if you really want to inherit from it and implement a custom destructor for your inherited one? Are there ways to reduce the evilness? The problem is that if you cast to std::vector and then destruct it, that the destructor of the inheriting class isn't called, is this correct? Are there other problems too? If so, can it be avoided that you can cast to std::vector somehow?

Share on other sites
You can inherit non-publically.

Share on other sites
Quote:
 Original post by LodeInheriting from std::vector is evil because it hasn't got a virtual destructor.However, what if you really want to inherit from it and implement a custom destructor for your inherited one? Are there ways to reduce the evilness?The problem is that if you cast to std::vector and then destruct it, that the destructor of the inheriting class isn't called, is this correct? Are there other problems too?If so, can it be avoided that you can cast to std::vector somehow?

Prefer composition to inheritance. STL containers really weren't meant to be extended in this manner, so it's better not to use them in this way.

You can implement an STL-like interface with simply delegates the functionality to the vector member. It's more typing, but less risky.

Share on other sites
You don't need the virtual destructor, unless you're going to use polymorphic behaviour (ie. use your deriving vector through a std::vector<T>*).

[Edited by - Koen on March 7, 2008 9:48:41 AM]

Share on other sites
Take a look at the source of std::queue, which wraps pretty much any other standard container to provide a uniform interface. Hopefully you can do something similar and avoid inheriting from std:vector.

Share on other sites
Quote:
 Original post by SiCraneYou can inherit non-publically.

Something like this:
Quote:
 template class vector_base : std::vector{public: virtual ~vector_base(){} using std::vector::...};

Now you've got a class that behaves exactly like std::vector, except it has a virtual d'tor.

Quote:
 You don't need the virtual destructor, unless you're going to use polymorphic behaviour (ie. use your deriving vector through a std::vector*).link: c++ faq

It's frowned upon, as you typically wouldn't get any error or warning from casting to std::vector<T>* and you may only realsie your mistake when bug reports start rolling in.

Share on other sites
Quote:
 Original post by KoenYou don't need the virtual destructor, unless you're going to use polymorphic behaviour (ie. use your deriving vector through a std::vector*).link: c++ faq

The downside is in the case where it doesn't work, it's a very subtle bug that's not easy to detect.

Share on other sites
The only problem I can think of is if you start doing stuff like this:
class MyDerivingVector: public std::vector<T>  {  public:    ~MyDerivingVector()      {      //...      }  };std::vector<T>* base_pointer = new MyDerivingVector;//...delete base_pointer; // --> o-ow!!

But you could argue that a c++ developer should know this is not something you should do: a vector container is not a polymorphic object. The same thing happens for example with geometric point and vector classes with templatized bases, and in that case no one seems to mind.
Or am I missing something else, that can cause bugs/problems?

Share on other sites
Quote:
 Original post by KoenThe only problem I can think of is if you start doing stuff like this:class MyDerivingVector: public std::vector { public: ~MyDerivingVector() { //... } };std::vector* base_pointer = new MyDerivingVector;//...delete base_pointer; // --> o-ow!!But you could argue that a c++ developer should know this is not something you should do: a vector container is not a polymorphic object. The same thing happens for example with geometric point and vector classes with templatized bases, and in that case no one seems to mind.Or am I missing something else, that can cause bugs/problems?

There's really no reason to inherit from std::vector if it's not intended to be used polymorphically. Inherit for interface, not implementation. It's valid to assume that a derived class can be safely used polymorphically, if you derive from it. What's described above is an anti-pattern and a violation of proper object-oriented practices.

For more information, see C++ Coding Standards, Item 34: Prefer Composition to Inheritance and Item 35: Avoid inheriting from classes that were not designed to be base classes.

1. 1
2. 2
frob
20
3. 3
Rutin
17
4. 4
5. 5

• 13
• 10
• 9
• 18
• 9
• Forum Statistics

• Total Topics
632555
• Total Posts
3007035

×