gpt4 book ai didi

c++ - dynamic_cast 是否需要多态类?

转载 作者:行者123 更新时间:2023-11-30 02:43:49 32 4
gpt4 key购买 nike

我已经对此进行了一些搜索,但只是为了确保:

使用dynamic_cast 将基类指针转换为派生类指针需要基类是多态的吗?不然连编译都编译不了?

谢谢。

最佳答案

您可以使用 dynamic_cast 将派生类转换为非多态基类。但是您不能将非多态基动态转换为派生类。

标准解释(§5.2.7):

(1) The result of the expression dynamic_cast(v) (...)

(5) If T is “pointer to cv1 B” and v has type “pointer to cv2 D” such that B is a base class of D, the result is a pointer to the unique B subobject of the D object pointed to by v.

(6) Otherwise, v shall be a pointer to or a glvalue of a polymorphic type

多态在这里的意思是非常谦虚的:“一个声明或继承虚函数的称为多态类。"(§10.3/1)。

关于c++ - dynamic_cast 是否需要多态类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25838258/

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