TITLE: What does "virtual constructor" mean? PROBLEM: ehsjony@ehs.ericsson.se (Jonas Nygren), 8 Nov 93 Reading an article on Booch Components I encountered the term 'virtual constructor' but never managed to quite understand what this was. I suppose it is not a language construct but rather a coding convention. Could anybody help me with an explanation - what is a virtual constructor? RESPONSE: kanze@us-es.sel.de (James Kanze) It is not a language construct; in C++ a constructor cannot be virtual. What is referred to, I suppose, is a technique of creating an object whose type is only determined at run-time. There are several different ways of doing this; my own preferred way is for each class to provide a static function 'createNew', which returns a pointer to the base class, and create a map from a String (with the class name) to the corresponding 'createNew' function. Typically, the map will be initialized automatically; the actual 'createNew' will be a (static) member of a private, nested class, and actual class will contain a static member of this class. The constructor for the private, nested class will then register the 'createNew' function with the map. (Most class libraries will provide a map container class, also sometimes called an associative array.) A frequent variation is to make the private nested class a functor, derived from a common base factory class. In this case, the class constructor registers itself, rather than the function, with the map, and the 'createNew' function is called virtually through the instance of the functor. [ There is also a discussion of ways to implement virtual constructors in Coplien's book. -adc ]