gpt4 book ai didi

c++ - Dynamic_cast 不需要执行运行时检查吗?

转载 作者:行者123 更新时间:2023-11-30 00:53:09 34 4
gpt4 key购买 nike

<分区>

引用自 C++ Gotchas 中的第 45 条:

First a dynamic_cast is not necessarily dynamic, in that it may not perform a runtime check. When performing a dynamic_cast from a derived class pointer (or reference) to one of its public base classes no runtime check is needed, because the compiler can determine statically that the cast will succeed. Of course, no cast of any kind is needed in this case, since conversion from a derived class to its public base classes is predefined.

我认为上面的描述是通常使用 dynamic_cast 的地方(因此会进行运行时检查??)。

有人可以解释一下上述引用与“典型”需要使用 dynamic_cast 之间的区别吗?这让我对何时需要使用 dynamic_cast 以及为什么在上述情况下不需要使用它感到困惑。

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