Extending a class template

How do I do to extend a template class like a vector? The code below doesn't work. The compiler whines about "Vector" being not a template.

template <typename T>
class Vector<T> : public std::vector<T>
{
public:
    void DoSomething()
    {
        // ...
    }
};

      

+2


a source to share


2 answers


Your syntax is incorrect; you need to use:

template <typename T>
class Vector : public std::vector<T>

      



However, you should not propagate standard library containers through inheritance, if not for some other reason, because they do not have virtual destructors and are therefore inherently unsafe.

If you want to "improve" std::vector

, do it using composition (for example, with a member variable of the type std::vector

) or use non-member functions to provide additional functionality.

+19


a source


It has nothing to do with extending another class. The problem is your own derived class.

You define your class template like this:

template <typename T>
class Vector
{
...

      



but not

template <typename T>
class Vector<T>
{
...

      

+6


a source







All Articles