A constructor cannot be virtual

后端 未结 2 1706
孤街浪徒
孤街浪徒 2021-02-10 06:04

In one of the C++ tutorials in internet, i found out the below description on why a constructor cannot be virtual

We cannot declare a virtual constructor

2条回答
  •  生来不讨喜
    2021-02-10 06:36

    As Bjarne himself explains in his C++ Style and Technique FAQ:

    A virtual call is a mechanism to get work done given partial information. In particular, "virtual" allows us to call a function knowing only an 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.

提交回复
热议问题