When is a virtual constructor used?

Asked 2 years ago, Updated 2 years ago, 173 views

When is the virtual constructor used for C++?

Virtual constructors seem to be a really useless feature, but I don't know when. Isn't superclass constructor called automatically when inherited? Why bother using virtual

c++ virtual constructor

2022-09-22 22:04

1 Answers

To briefly summarize the Why don't we have virtual constructs? in the Bjarne Strustrup's C++ Style and Technique FAQ

The virtual constructor is used to reveal that this class was created to write to the interface, not to create the object.

A virtual call is a mechanism to get work done given partial information. In particular, "virtual" allows us to call a function knowing only any interfaces and not the exact type of the object. To create an object you need complete information. In particular, you need to know the exact type of what you want to create. Consequently, a "call to a constructor" cannot be virtual.


2022-09-22 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.