You are here:

C++/linked list


linked list can contain data of several types?

Hmmm, that depends!

Usually a linked list will be designed to hold objects of the same type or related types. If the list nodes hold data by value - that is they have a data member that is the element's value then they hold objects of a single type. If the list nodes only refer to the object they hold - generally by (smart) pointer in C++ - then they can contain objects of types that are related by being sub-types of the type of object referred to (pointed to) in each node, which then would be a pointer to a base class type.

The std::list implementation for example holds items of all the same type by value. However this value can be a pointer - or for preference a C++11 std::shared_pointer or other C++ library container compatible smart pointer type and thus can refer to objects of related types if the pointer is a pointer to a base class type. For example you might use a std::list<std::shared_pointer<widget>> to hold (smart) pointers to various concrete widget types such as button, listbox, editbox, etc. accessed through the base widget type's interface.

Alternatively you could store objects in a type that is designed to hold an object of various types - see the Boost variant and any types for two examples:

The documentation for both Boost any and Boost variant show examples of use as elements of C++ library sequence containers - the Boost any Examples page even shows a std::list<boost::any> type alias. So although you technically have a list of elements of the same (value) type, the contents of each value object may vary.

Of course you could devolve into C style and specify that each linked list node's value is an untyped pointer - a void * - and thus points to an object of any (and unknown) type. However as the type of object pointed to by each node has been lost doing anything useful with the objects without further information somewhere is going to be difficult!! Such information could of course be built into the code - continuing in C style imagine a set of functions that operate on a specific type and therefore assume the objects pointed to in lists they manipulate are of this type. Maybe there are several such sets of functions that work with different object types. If so then woe betide you if you pass a list meant for use with one type's functions to a function handling another type - as this second function will assume the list nodes point to objects of the type it handles and not the type it actually handles. There will be no way to check for this before runtime - which will likely result in a crash or bad results and/or weird behaviour unless special care is taken (e.g. the functions use some sort of sanity checks to ensure they are working with the correct object type).

Hope this helps  


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]