Why do we not have a virtual constructor in C++?


Why does C++ not have a virtual constructor?

2/6/2012 8:38:04 AM

Hear it from the horse's mouth:).

From Bjarne Stroustrup's C++ Style and Technique FAQ Why don't we have virtual constructors?

A virtual call is a mechanism to get work done given partial information. In particular, "virtual" allows us to call a function knowing only any interfaces and not the exact type of the object. To create an object you need complete information. In particular, you need to know the exact type of what you want to create. Consequently, a "call to a constructor" cannot be virtual.

The FAQ entry goes on to give the code for a way to achieve this end without a virtual constructor.

2/13/2015 10:53:27 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow