[英]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
之后派生的mDerived
的Hello()
方法。
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.