gpt4 book ai didi

C++ dynamic_cast 异常

转载 作者:行者123 更新时间:2023-11-28 05:57:12 27 4
gpt4 key购买 nike

请帮助我理解奇怪的行为:

我在处理析构函数~MyLogicObject()时使用dynamic_cast from MyObject to MyLogicObject,但编译器抛出异常:非 rtti_object

我确定对象 MyObject 是多态类型。我哪里错了?

#ifndef MYOBJECT_H
#define MYOBJECT_H

#include <string>

class A
{
int a;
};

class B
{
int b;
};

class MyObject: public A,
public B// if comment this row, and don't use multi inheritable, everything will be fine
{
private: std::string name;
private: bool singleshot;


public: MyObject(void);

public: virtual ~MyObject(void);

protected: void Destroying(void);

public: std::string GetName(void);

public: virtual bool Rename(std::string _newName);
};

#endif

#include "MyObject.h"
#include "MyLogicObject.h"

MyObject::MyObject(void): singleshot(true)
{}


MyObject::~MyObject(void)
{
printf("\n~my object\n");
Destroying();
}


void MyObject::Destroying(void)
{
if(singleshot)
{
printf("\nexception!\n");
dynamic_cast<MyLogicObject*>(this);// exception: non_rtti_object
singleshot = false;
}
}


std::string MyObject::GetName(void)
{
return name;
}


bool MyObject::Rename(std::string _newName)
{
name = _newName;
return true;
}


#ifndef MYLOGICOBJECT_H
#define MYLOGICOBJECT_H
#include "MyObject.h"

class MyLogicObject: public virtual MyObject // if not use virtual inheritance (instead, use the standard inheritance), everything will be fine
{
public: MyLogicObject(void);

public: virtual ~MyLogicObject(void);

public: virtual void Update(float _delta = 0.0f);

// if reimplement virtual method of base class, everything will be fine
/*
public: virtual bool Rename(std::string _newName)
{
return MyObject::Rename(_newName);
}
*/
};

#endif

#include "MyLogicObject.h"


MyLogicObject::MyLogicObject(void)
{}


MyLogicObject::~MyLogicObject(void)
{
printf("\n~my logic object\n");
Destroying();
}



void MyLogicObject::Update(float _delta)
{}

#include <conio.h>
#include <stdio.h>
#include "MyLogicScene.h"


class C
{
int c;
};



class DerivedObject: public MyLogicObject,
public C// if comment this row, and don't use multi inheritable, everything will be fine
{
public: DerivedObject(void)
{}

public: virtual ~DerivedObject(void)
{
printf("~derived object: %s\n", GetName().c_str());
//Destroying(); // if call Destroying in this place, overything will be file
}
};


int main()
{
DerivedObject* object1 = new DerivedObject();
object1->Rename("object1");

printf("delete object1...\n");
delete object1;


getch();
return 0;
}

最佳答案

您正在尝试将基类 (MyObject) 类型的对象动态转换为派生类 (MyLogicObject)。并且 dynamic_cast 不允许这种转换,除非基类是多态的并且启用了 rtti。参见 this供引用。

因此,您基本上需要在编译器选项中启用 rtti。一旦完成,请确保 object1 是派生类 (MyLogicObject) 的完整对象,以便转换在不引发异常的情况下工作。

它也适用于相反的情况。例如,如果您尝试将派生类 (MyLogicObject) 类型的对象动态转换为基类 (MyObject)。

关于C++ dynamic_cast 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33918146/

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