You are here:

C++/Is it possible to inherit classes from STL classes?


Is it possible to inherit a class form an STL class.
Is there any memory issue if we r using that?
Is it related to virtual destructor concept?

Thanks & Regards;
John Samuel

The answer is yes and no. Yes you can inherit from an STL class (including the basic_string class template) but most are not designed to do so. That is the STL classes are user defined classes like any other and so the language is not going to prevent you inheriting from an STL class (remember however that many STL classes are not classes at all but class templates).

Any class (or class template) from which you can derive further classes (or class templates) would, as you ask, generally have a virtual destructor, and of course a bunch of virtual member functions intended to be overridden in sub-classes. This of course means that such a class would require all the baggage that goes with supporting virtual functions, and this is bad for efficiency so is not done for STL classes/class templates.

Thus you cannot extend STL types for use polymorphically - that is you cannot in general safely use types derived from STL types through pointers or references to their base types. In the specific question you ask, because they do not declare a virtual destructor deleting a an object sub-classed from an STL base class via a pointer to the base class would not cause the sub-class part of the object to be destroyed properly and depending on the specifics of the sub-class implementation might lose memory or cause other resources problems.

Therefore in general it is more common to create, say, your own container types by wrapping your own class (or class template) around an STL container, and supplying only those operations you actually require - that is the STL container is a member of your class/class template. See for example the STL container adapters such as queue, priority_queue and stack - each of these contains a member of the container type they are adapting (passed as a parameter to the container adapter class template), they do not derive from them.

On the other hand you could safely perform some sorts of derivation from STL classes. For an example see "Exceptional C++" by Herb Sutter, Items 2 and 3 in which he derives a specialisation of the char_traits type used to define the characteristics of character types to the likes of basic_string. In these examples he creates a case insensitive string type by specialising the supplied char_traits for the char type to perform case insensitive comparisons and the operations eq, lt, compare and find are replaced (note I say replaced not overridden, they are _not_ virtual so the derived class versions hide those in the base class).

And of course the STL does provide for some facilities that are supposed to use derivation. One obvious example is the use of the iterator class template (in fact it is usually a struct template) for use in creating your own iterator types. For further information see "The C++ Standard Library a Tutorial and Reference" by Nicolai M. Josuttis, sections 7.5.2 and 7.5.1. However the iterator template class (struct) contains no data or operations, only types, as seen from the definition given in the ISO C++ Standard (section 24.3.2 Basic Iterator):

   namespace std
       template<class Category, class T, class Distance = ptrdiff_t,
       class Pointer = T*, class Reference = T&>
       struct iterator
         typedef T value_type;
         typedef Distance difference_type;
         typedef Pointer pointer;
         typedef Reference reference;
         typedef Category iterator_category;

So the answer is no in general, but yes occasionally.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.

[an error occurred while processing this directive]