You are here:

C++/Can we make a constructor virtual?



Can we make a constructor virtual?


So if you could how would this work?

The polymorphic behaviour that virtual member functions allow us to achieve work through base pointers or references to _existing_ objects.

How then can you polymorphically directly create an object? Where is the base pointer or reference that refers to a specific object type instance that can be used?

Oops, we do not have one because we have not created the specific object type instance yet!

On the other hand we can have such references or pointers available when it comes time to destroy an object so we can have virtual destructors.

No. Constructors are always non-virtual and refer to the object type in which they are defined. By default a derived class instance will call its base classes' default constructors before it executes to ensure they are all initialised before any derived initialisation is done. In fact during construction the virtual function call mechanism is also initialised so calling virtual functions from a base class constructor will not call a derived override because that part of the object has not yet been initialised (a similar case exists during destruction: objects are destroyed derived to base, in the reverse order of construction).

However, you might have noticed that I previously used the term "directly create an object" - meaning directly via a constructor. However we can create "virtual constructor" like virtual member functions. These would work by creating new objects from existing objects. Two variations are obviously possible for all cases: a virtual create method that creates a new object using the default constructor and a virtual clone method that creates new objects using the copy constructor (thereby making a clone of the object on which clone is called).

Here is a simple example:

   class AbstractBase
       virtual ~AbstractBase() {}
       virtual AbstractBase * Create() = 0;
       virtual AbstractBase * Clone() = 0;

   // ...


   class SomeType : public AbstractBase
       virtual SomeType * Create();
       virtual SomeType * Clone();

   // ...


   SomeType * SomeType::Create()
       return new SomeType;

   SomeType * SomeType::Clone()
       return new SomeType(*this);

   class SomeOtherType : public AbstractBase
       virtual SomeOtherType * Create();
       virtual SomeOtherType * Clone();

   // ...


   SomeOtherType * SomeOtherType::Create()
       return new SomeOtherType;

   SomeOtherType * SomeOtherType::Clone()
       return new SomeOtherType(*this);

The base class AbstractBase has an empty virtual destructor and defines the two pure virtual functions Create and Clone, each of which returns a pointer to an AbstractBase.

There are two classes derived from AbstractBase: SomeType and SomeOtherType. Each of these classes implements the Create and Clone virtual functions. Note that the return types are declared to be of types SomeType * and SomeOtherType * rather than AbstractBase *. This feature is called covariant return types. It is permissable because AbstractBase * is a pointer and is an unambiguous base class of the return types specified by the functions in SomeType and SomeOtherType. However some older compilers (e.g. Microsoft Visual C++ 6 and earlier) do not support this feature.

The implementation of the Create and Clone functions is very formulaic: they just return a new instance of the class they are overridden in. In the case of Create the default constructor is used, in the case of Clone the copy constructor is used and *this is passed to it (this is a pointer and copy constructors take either references to (usually const) objects or an object itself (i.e. pass by value) so we have to dereference this before passing it).

Note that you can use variations on a theme here. Maybe the classes in your class hierarchy all support some constructor that takes a specific set of parameters. In this case the Create virtual function would also take these parameters and pass them on to the class constructors. In fact you could have more than one Create method signature if required (overloading and overriding combined!).

You can find a similar answer and example in the C++ FAQ LITE at, in section 20, specifically 20.8.

I would recommend you refer to this FAQ before posting questions as it may well be that your question is answered there.  


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.