gpt4 book ai didi

c++ - 构造函数不能是虚拟的

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:50:11 26 4
gpt4 key购买 nike

C++ tutorials in internet 之一中,我发现了以下关于为什么构造函数不能是虚拟的描述

We cannot declare a virtual constructor. We should specify the exact type of the object at compile time, so that the compiler can allocate memory for that specific type.

这个描述是否正确?

我对这个短语特别感到困惑:以便编译器可以分配
该特定类型的内存

最佳答案

正如 Bjarne 自己在他的 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.

关于c++ - 构造函数不能是虚拟的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8506938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com