简体   繁体   English

将基础 class 转换为派生 class 时,dynamic_cast 失败

[英]dynamic_cast fails when cast a base class to derived class

I have two classes, base class and a derived class.我有两个类,基本 class 和派生 class。 The base class has a virtual method.基础 class 有一个虚方法。

Here is my test example:这是我的测试示例:

class Base
{
public:
    virtual void Hello() { cout << "-> Hello Base" << endl; }
};

class Derived: public Base
{
public:
    void Hello() { cout << "-> Hello Derived" << endl; }
};

int main()
{
    Base *mBase = new Base;
    // something to do 
    ....

    Derived *mDerived = dynamic_cast<Derived*>(mBase);
    mDerived->Hello();

    return 0;
}

I'm looking to use the Hello() method of the class derived after the cast of mBase to mDerived .我希望使用在将 mBase 转换为mBase之后派生的mDerivedHello()方法。

But the problem is that when I try to use dynamic_cast it will crash the application, if not if I use reinterpret_cast the Hello() method of the Base class will be called.但问题是,当我尝试使用dynamic_cast时,它会使应用程序崩溃,如果不是,如果我使用reinterpret_cast ,则将调用Base class 的Hello()方法。

Result in the case dynamic_cast :情况下的结果dynamic_cast

Segmentation fault (core dumped)

Result in the case dynamic_cast :情况下的结果dynamic_cast

-> Hello Base

dynamic_cast fails when cast a base class to derived class将基础 class 转换为派生 class 时,dynamic_cast 失败

This is what is supposed to happen.这是应该发生的事情。 When you dynamic cast a pointer to an object whose dynamic type is not the casted type, then you get a null pointer as the result.当您动态转换指向动态类型不是转换类型的 object 的指针时,您会得到 null 指针作为结果。

In your example, you indirect through the null pointer and attempt to call a member function which results in undefined behaviour.在您的示例中,您间接通过 null 指针并尝试调用成员 function 导致未定义的行为。

When using dynamic cast, you must always check whether you got null or not.使用动态转换时,您必须始终检查您是否获得了 null。

if I use reinterpret_cast...如果我使用 reinterpret_cast ...

Then the behaviour will still be undefined because you'll be indirecting through a pointer to an object that does not exist.然后行为仍然是未定义的,因为您将通过指向不存在的 object 的指针进行间接。 Unless you create an instance of the derived class, you cannot call its non static member function.除非创建派生 class 的实例,否则不能调用其非 static 成员 function。

You can convert a base instance into a derived one like this for example:您可以将基本实例转换为派生实例,例如:

Base b;
Derived d = b;

What happens is that the base sub object of the derived instance is copy initialised from b .发生的情况是派生实例的基本子 object 是从b初始化的副本。

Two problems in your code: Base should have a virtual destructor so Dervied isntances can be properly destructed via a pointer to Base .您的代码中有两个问题: Base应该有一个虚拟析构函数,因此可以通过指向Base的指针正确地析构Dervied实例。 Then, you are not constructing an object of type Derived , so the cast cannot succeed.然后,您没有构造Derived类型的 object ,因此强制转换无法成功。 dynamic_cast can fail and you should check its result for not being a nullptr . dynamic_cast可能会失败,您应该检查它的结果是否不是nullptr Also you forgot to delete the created object.此外,您忘记删除创建的 object。

If you do create an instance of type Derived your code works:如果您确实创建了Derived类型的实例,则您的代码可以工作:

#include <iostream>
using std::cout;
using std::endl;

class Base
{
public:
    virtual void Hello() { cout << "-> Hello Base" << endl; }
    virtual ~Base(){}
};

class Derived: public Base
{
public:
    void Hello() { cout << "-> Hello Derived" << endl; }
};

int main()
{
    Base* mBase = new Derived;
    Derived *mDerived = dynamic_cast<Derived*>(mBase);
    if (mDerived) mDerived->Hello();
    delete mBase;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将派生类转换为基类时,dynamic_cast失败 - dynamic_cast fails when cast a derived class to base class 避免对派生类(Cast Derived类)进行dynamic_cast - Avoid dynamic_cast with derived classes (Cast Derived class) 访问存储在向量中的已知派生类对象时,使用dynamic_cast或static_cast有什么问题 <base*> ? - What's wrong with using dynamic_cast or static_cast when accessing known derived class objects stored in vector<base*>? 从基类转换到子类时,dynamic_cast 失败 - dynamic_cast is failing when casting from base to child class C ++ dynamic_cast基类指针到派生类指针 - C++ dynamic_cast base class pointer to derived class pointer 使用`dynamic_cast`来推断在基类上定义并在派生类上实现的成员函数的参数类型是正确的吗? - Is correct to use `dynamic_cast` to infer the type of an argument of a member function defined on the base class and implemented on a derived class? C ++如何在不使用static_cast或dynamic_cast的情况下从基类指针访问派生类成员? - C++ How to access derived class member from base class pointer without using a static_cast or dynamic_cast? `dynamic_cast`从Base到Derived - `dynamic_cast` from Base to Derived dynamic_cast到派生类型失败,为什么? - dynamic_cast to derived type fails, Why? 在派生类中实现虚函数时避免使用dynamic_cast - Avoiding dynamic_cast in implementation of virtual functions in derived class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM