Virtual Constructors in C : Demystified
Why is the notion of virtual constructors foreign to the C programming language? This question has puzzled many, and it is time to unveil the answer to this enigma.
According to C pioneer Bjarne Stroustrup, a virtual constructor is conceptually not feasible because it contradicts the nature of object creation. A virtual call is designed to handle situations where the specific type of the object being called is not fully known. However, object creation necessitates the knowledge of the exact type required. Therefore, the very purpose of a virtual constructor is at odds with the fundamental mechanism of object construction.
To achieve similar functionality, Stroustrup suggests an alternative approach that mimics the behavior of a virtual constructor without its inherent limitations. This workaround involves leveraging a method called "double dispatch," which allows for type verification during runtime.
The above is the detailed content of Why Are Virtual Constructors Not Possible in C ?. For more information, please follow other related articles on the PHP Chinese website!