I read about virtual constructors are used for implementing some design patterns, but didn't understood any need of virtual constructors. So what are virtual constructors and why we really need them?
Virtual Constructor in C++ The virtual mechanism works only when we have a base class pointer to a derived class object. In C++, the constructor cannot be virtual, because when a constructor of a class is executed there is no virtual table in the memory, means no virtual pointer defined yet.
[20.8] What is a "virtual constructor"? An idiom that allows you to do something that C++ doesn't directly support. You can get the effect of a virtual constructor by a virtual clone() member function (for copy constructing), or a virtual create() member function (for the default constructor).
Constructor can not be virtual, because when constructor of a class is executed there is no vtable in the memory, means no virtual pointer defined yet. Hence the constructor should always be non-virtual.
A constructor can not be virtual because when the constructor of a class is executed, there is no virtual table in the memory, which means no virtual pointer defined yet. Hence the constructor should always be non-virtual.
In most programming languages, afaik, you cannot find virtual constructors. Which override of a virtual members are evaluated at runtime after an object is constructed, but in most languages you need to know the actual class when constructing the instance. Therefore virtual constructors make no sense in these languages.
In .NET, you can get a similar solution through reflection, i.e. you can construct an object through an instance of the Type class that represents the object you want to construct. And with generic support, you can also achieve something similar, but it's not virtual constructors.
The only programming language that I have worked with that have true virtual constructors is Delphi. In Delphi, there is a specific "Metaclass type", i.e. a specific programming construct that represents a metaclass (whereas in .NET, the meta class, the Type
class, is just an instance of a normal class). So if you have a class called TMyClass
- Deplhi naming conventions ;)
TMyClass : Class ...
You can declare the metaclass like this
TMyMetaClass : class of TMyClass
Now, you can declare a variable that is of TMyMetaClass
type,
MyMetaClassVariable : TMyMetaClass
...
// Assign the meta class to refer to our concrete class
MyMetaClassVariable := TMyClass;
And you can construct a new instance through this variable
MyClassInstance := MyMetaClassVariable.Create();
Now, the MyMetaClassVariable
can refer to any class that is either TMyClass
or a specialization thereof. If the constructor is declared virtual, then the variable will be constructed with an instance of that specific class.
In the same way, you can declare virtual static methods in Delphi, and call them through an instance of the metaclass.
So the other question? Why do we need them? Well, in Delphi, they solve some of the same problems as the Type
class in .NET, allowing you to construct objects where you don't know the class name at design time.
For example, when you design a form and you put in a bunch of controls, this data has to be serialized by the designer, and deserialized. When the form is deserialized, then it is actually the metatypes that are read, and the correct instances (be it TextBox
, ComboBox
, etc) are constructed by calling the virtual constructor on the metatype.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With