<p>Is there any need of Virtual Constructors? If so can any one post a scenario?</p>

[{'answer_id': 70589, 'author': 'grigy', 'author_id': 1692070, 'author_profile': 'https://Stackoverflow.com/users/1692070', 'pm_score': 1, 'selected': False, 'text': '<p>In what language? In C++ for example the constructors can not be virtual.</p>\n'}, {'answer_id': 70597, 'author': 'Marcin Gil', 'author_id': 5731, 'author_profile': 'https://Stackoverflow.com/users/5731', 'pm_score': 2, 'selected': False, 'text': '<p>As always: look up at <a href="http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8" rel="nofollow noreferrer">C++ FAQ lite: virtual functions</a>.</p>\n\n<p>It will explain not only "virtual constructor" but destructors/functions too!</p>\n\n<p>This of course, if you wanted C++ in the first place...</p>\n'}, {'answer_id': 70618, 'author': 'grigy', 'author_id': 1692070, 'author_profile': 'https://Stackoverflow.com/users/1692070', 'pm_score': 0, 'selected': False, 'text': '<p>The constructor can not be virtual by definition. At the time of constructor call there is no object created yet, so the polymorphism does not make any sense.</p>\n'}, {'answer_id': 70634, 'author': 'Mark Ingram', 'author_id': 986, 'author_profile': 'https://Stackoverflow.com/users/986', 'pm_score': 3, 'selected': False, 'text': '<p>If you are talking about virtual destructors in C++ (there isn\'t any such thing as virtual constructors) then they should always be used if you are using your child classes polymorphically.</p>\n\n<pre><code>class A\n{\n  ~A();\n}\n\nclass B : public A\n{\n  ~B();\n}\n\nA* pB = new B();\ndelete pB; // NOTE: WILL NOT CALL B\'s destructor\n\nclass A\n{\n  virtual ~A();\n}\n\nclass B : public A\n{\n  virtual ~B();\n}\n\nA* pB = new B();\ndelete pB; // NOTE: WILL CALL B\'s destructor\n</code></pre>\n\n<p><strong>Edit:</strong> Not sure why I\'ve got a downvote for this (would be helpful if you left a comment...) but have a read here as well</p>\n\n<p><a href="http://blogs.msdn.com/oldnewthing/archive/2004/05/07/127826.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/oldnewthing/archive/2004/05/07/127826.aspx</a></p>\n'}, {'answer_id': 70693, 'author': 'Tim Jarvis', 'author_id': 10387, 'author_profile': 'https://Stackoverflow.com/users/10387', 'pm_score': 2, 'selected': False, 'text': '<p>Delphi is one language that supports virtual constructors.</p>\n\n<p>Typically they would be used in a class factory type scenario where you create a meta type i.e. that is a type that describes a type. You would then use that meta type to construct a concrete example of your descendant class</p>\n\n<p>Code would be something like....</p>\n\n<pre><code>type\n  MyMetaTypeRef = class of MyBaseClass;\n\nvar\n  theRef : MyMetaTypeRef;\n  inst : MyBaseClass;\nbegin \n  theRef := GetTheMetaTypeFromAFactory(); \n  inst := theRef.Create(); // Use polymorphic behaviour to create the class\n</code></pre>\n'}, {'answer_id': 70697, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 2, 'selected': False, 'text': '<p>There are plenty of scenarios, for example if you want to create GUIs for more than one environment. Let\'s say you have classes for controls (“widgets”) but each environment actually has its own widget set. It\'s therefore logical to subclass the creation of these widgets for each environment. The way to do this (since, as has been unhelpfully pointed out, constructors can\'t actually be virtual in most languages), is to employ an <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern" rel="nofollow noreferrer">abstract factory</a> and the above example is actually the standard example used to describe this design pattern.</p>\n'}, {'answer_id': 70822, 'author': 'easeout', 'author_id': 10906, 'author_profile': 'https://Stackoverflow.com/users/10906', 'pm_score': 0, 'selected': False, 'text': '<p>In C++, there\'s no reason for constructors to ever be virtual, because they are static functions.  That means they\'re statically bound, so you have to identify the very constructor function you\'re calling in order to call it at all.  There\'s no uncertainty and nothing virtual about it.</p>\n\n<p>This also means that, no matter what, you need to know the class that your object is going to be.  What you can do, however, is something like this:</p>\n\n<pre><code>Superclass *object = NULL;\nif (condition) {\n    object = new Subclass1();\n}\nelse {\n    object = new Subclass2();\n}\nobject.setMeUp(args);\n</code></pre>\n\n<p>... have a virtual function and call it after constructon.  This is a standard pattern in Objective-C, in which first you call the class\'s "alloc" method to get an instance, and then you call the initilializer that suits your use.</p>\n\n<p>The person who mentioned the Abstract Factory pattern is probably more correct for C++ and Java though.</p>\n'}, {'answer_id': 11664488, 'author': 'Homer6', 'author_id': 278976, 'author_profile': 'https://Stackoverflow.com/users/278976', 'pm_score': -1, 'selected': False, 'text': '<p>In C++, all constructors are implicitly virtual (with a little extra). That is, the constructor of the base class is called before that of the derived class. So, it\'s like they\'re sort of virtual. Because, in a virtual method, if the derived class implements a method of the same signature, only the method in the derived class is invoked.</p>\n\n<p>However, <strong>in a constructor, BOTH METHODS ARE INVOKED</strong> (see example below).</p>\n\n<p>For a more complete explanation of why this is so, please see Item 9 of Effective C++, Third Edition, By Scott Meyers (Never call a virtual function during construction or destruction). The title of the item may be misleading in relation to the question, but if you read the explanation, it\'ll make perfect sense.</p>\n\n<pre><code>#include &lt;iostream&gt;\n#include &lt;vector&gt;\n\nclass Animal {\n\n    public:\n\n        Animal(){\n            std::cout &lt;&lt; "Animal Constructor Invoked." &lt;&lt; std::endl;\n        }\n\n        virtual void eat() {\n            std::cout &lt;&lt; "I eat like a generic animal.\\n";\n        }\n\n        //always make destructors virtual in base classes\n        virtual ~Animal() {\n\n        }\n\n};\n\nclass Wolf : public Animal {\n\n    public:\n\n        Wolf(){\n            std::cout &lt;&lt; "Wolf Constructor Invoked." &lt;&lt; std::endl;\n        }\n\n        void eat() {\n            std::cout &lt;&lt; "I eat like a wolf!" &lt;&lt; std::endl;\n        }\n\n};\n\n\nint main() {\n\n    Wolf wolf;\n    std::cout &lt;&lt; "-------------" &lt;&lt; std::endl;\n    wolf.eat();\n\n}\n</code></pre>\n\n<p>Output:</p>\n\n<pre><code>Animal Constructor Invoked.\nWolf Constructor Invoked.\n-------------\nI eat like a wolf!\n</code></pre>\n'}, {'answer_id': 14971562, 'author': 'rockstar', 'author_id': 803649, 'author_profile': 'https://Stackoverflow.com/users/803649', 'pm_score': -1, 'selected': False, 'text': '<p>Virtual constructors dont make sense in C++ . THis is because in C++ constructors do not have a return value . In some other programming languages this is not the case . In those languages the constructor can be called directly and the constructor has a return value . This makes them useful in implementing certain types of desgin patterns . In C++ however this is not the case . </p>\n'}]
['https://Stackoverflow.com/questions/70575', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11554/']