Home > Backend Development > C++ > Why Are Virtual Constructors Not Possible in C ?

Why Are Virtual Constructors Not Possible in C ?

Barbara Streisand
Release: 2024-11-30 20:52:15
Original
791 people have browsed it

Why Are Virtual Constructors Not Possible in C  ?

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template