You are here:

C++/cast a super array type to a sub array type ?



Can we cast a super array type to a sub array type ? the super array stores super class objects, while the sub array type stores sub class objects.


First apologies for taking a bit to get back to you. I have been away for a little longer that planned and have only just now got back to my workstation.

No. Not meaningfully or safely in general, and as far as I know from the standard and as claimed by g++ quite clearly if you try: ISO C++ forbids casting to an array type!

Consider what you have asked actually means in terms of just converting the types of the array elements, rather than the array types themselves. You cannot in general meaningfully convert a super type, say T_super to a sub type, say T_sub, as the subclass may well hold additional state than is present in the superclass. Remember a superclass is a base class and a subclass of this class will be derived from it, see Here is an obvious simple example:

class T_super
   virtual ~T_super() {}

// ...

   int  base_state;

class T_sub : public T_super
// ...

   int  additional_state;

If I have a T_super object how do I convert it to a T_sub object? Where does the additional_state member come from?

To look at it another way. Remember that derivation is a is-a relationship thus a T_sub object is a T_super object _but_ a T_super object is NOT a T_sub object.

Recall that in C++, unlike say Java, class objects have value semantics, not reference semantics. If you want reference semantics in C++ you use C++ pointer and/or reference types. Objects do not have to be created on the heap in C++. If they are, using new (like Java) then you receive a pointer to an object (or array of objects), and have reference semantics, like Java (although of course unlike Java in C++ you are responsible for ensuring the object is destroyed by ensuring it is deleted at some point).

Using arrays of pointers to base classes (i.e. superclasses) allows references to collections of related subclass objects to be used in the same array polymorhically. A typical example would be widgets on a GUI dialog box - edit boxes, buttons, check boxes, radio buttons etc. Pointer 'references' to each widget could be held in an array or other collection and accessed polymorphically, and presumably each widget would have a common base set of operations and attributes as well as their own specific operations and attributes. At some point a down cast would need to be performed from the pointer to a base widget to a pointer to a specific widget type. If you are sure you know what the actual specific widget referenced by a base pointer is then you can use a static_cast to down cast to a pointer of this specific type. Otherwise take a bit of a runtime performance hit and play safe and use dynamic_cast and _check_ that the cast succeeded.  


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


©2016 All rights reserved.