gpt4 book ai didi

c++ - 如何使用 dynamic_cast 正确向下转换?

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

我对 dynamic_cast 很困惑.来自 C++ Primercppreference 的 Material (规则 5)不能帮助我理解。 (cppreference 比书难得多,我都非常仔细地阅读了它们)

来自 C++ Primer 5th:
dynamic_cast<type*>(e)

In all cases, the type of e must be either a class type that is publicly derived from the target type, a public base class of the target type, or the same as the target type. If e has one of these types, then the cast will succeed...

下面是我对上面引用的文字的理解:

(基类有虚函数)

dynamic_cast成功如果:

  1. e是从 type 继承的公共(public)派生类. e是 child 。上行。
  2. etype 的基类? type是 child 。沮丧。
  3. etype 相同.侧播?

示例代码:

#include <iostream>
using namespace std;

struct A {
virtual void foo() {}
};

struct B : A {

};

struct C : B {

};

int main()
{
A* pa = new B;
if (C* pc = dynamic_cast<C*>(pa)) {
cout << "1"; //B is a base class of C
}
return 0;
}

我不明白为什么这种沮丧会失败,我认为它满足条件 2.规则 5)(来自 cppreference)。


如果这本书错了(又该死),有人会详细说明吗rule 5)来自cppreference?没有例子我无法完全理解它说的是什么......

最佳答案

这是带有我注释的 cppreference 的规则:

5) If expression is a pointer or reference to a polymorphic type Base, and new_type is a pointer or reference to the type Derived a run-time check is performed:

这适用。 BC 的基础。

a) The most derived object pointed/identified by expression is examined. If, in that object, expression points/refers to a public base of Derived, and if only one subobject of Derived type is derived from the subobject pointed/identified by expression, then the result of the cast points/refers to that Derived subobject. (This is known as a "downcast".)

pa 指向的最派生对象是 B 类型。

尽管 BC 的公共(public)基础,但 pa 指向的特定实例不是 B 的实例 C 实例的基础子对象。指向的 B 实例是一个“具体”对象。所以,这种情况不适用。

一个例子:

C  c;
B* bp = &c; // bp points to base subobject of C
C* cp = dynamic_cast<C*>(bp);
assert(cp);

B b2;
B* bp2 = &b2; // bp does not point to a base subobject
C* cp2 = dynamic_cast<C*>(bp2);
assert(!cp2);

b) Otherwise, if expression points/refers to a public base of the most derived object, and, simultaneously, the most derived object has an unambiguous public base class of type Derived, the result of the cast points/refers to that Derived (This is known as a "sidecast".)

pa 没有指向基类为C 的最派生对象,所以这种情况不适用。

侧投的例子:

struct base {
virtual ~base(){}; // for polymorphism
};
struct left : base {};
struct right : base {};
struct derived : left, right {};

derived d;
left* l = &d;
right* r = dynamic_cast<right*>(l);

c) Otherwise, the runtime check fails. If the dynamic_cast is used on pointers, the null pointer value of type new_type is returned. If it was used on references, the exception std::bad_cast is thrown.

5a 和 5b 的情况都不适用,所以这个“否则”的情况 5c 适用。


  1. e is same as type. Sidecast?

不是旁白。旁播在 5b 中进行了解释。转换为同一类型只是身份转换(很少用,因此也不是常用术语)。


可能是书中attempt的条件描述了转换是否良构。虽然,“然后 Actor 会成功” 显然似乎暗示更多。引用的规则对于描述转换在运行时是否成功而言是正确的。

如果整个程序是良构的,那么编译器必须编译该程序。如果表达式格式不正确,则编译器必须给您一条诊断消息,说明您做错了。

您展示的示例程序格式正确,必须成功编译。它确实可以在我的系统上编译。

关于c++ - 如何使用 dynamic_cast 正确向下转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52556957/

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