将指针从基本类型转换为子类型 [英] Casting pointer from base type to child type
问题描述
我正在为我的项目构建一个简单的游戏设计。我有以下类:
I'm building a simple game design for a project of mine. I have the following classes:
class Character
{
public:
virtual void Display();
virtual void SetParameters( char* param, ... );
};
class NonPlayableCharacter : public Character
{
public:
virtual void Display();
virtual void SetParameters( char* paaram, ... );
int GetNPCState();
}
然后我有一堆从Character或NonPlayableCharacter派生的类。我这样定义:
And then I have a bunch of classes that derive either from Character or NonPlayableCharacter. I define it like so:
std::vector<Character*> _allChar;
我的问题是,在任何给定的时间,我想对一个元素执行一些操作矢量。所以从向量中获取一个元素我不能直接调用方法 GetNPCState()
,因为向量中的元素是Character *类型。这样做:
My problem is that at any given time I would want to perform some operation on one of the element of the vector. So getting an element out of the vector I can't directly call the method GetNPCState()
because the element in the vector are of Character* type. So doing this:
_allChar[0]->GetNPCState();
无法使用。所以我试着用着名的dynamic_cast:
doesn't work. So I tried doing it with the famous dynamic_cast:
NonPlayableCharacter* test = dynamic_cast<NonPlayableCharacter*>(_allChar[0]);
test->GetNPCState();
最后一次尝试的问题是 GetNPCState()
崩溃,因为对象是null,我知道一个事实(通过调试)_allChar [0]不为null。
The problem with this last attempt is that GetNPCState()
crashes because the object is null, and I know for a fact (via debugging) that _allChar[0] isn't null.
推荐答案
在C ++中有多种类型的转换(4),其中2个是感兴趣的:
There are several types of casts in C++ (4), of which 2 are of interests here:
-
static_cast
dynamic_cast
在运行时检查您是否猜到了
static_cast
assumes that you know what you are doingdynamic_cast
checks, at runtime, that you "guessed" right
>注意:简化,因为 dynamic_cast
也允许涉及虚拟基础的交叉转型和转换。
Note: simplified, as dynamic_cast
also allows cross-casts and casts involving virtual bases.
根据目标的性质,有 dynamic_cast
的3个版本:
There are 3 versions of dynamic_cast
, really, depending on the nature of the target:
- 如果目标是指针(即
dynamic_cast <如果检查失败,则
dynamic_cast
返回一个空指针 - 最后,作为特殊情况,如果目标是
void *
,则dynamic_cast
,而是返回完整对象的地址
<如果目标是引用(即
dynamic_cast ),则如果检查失败 dynamic_cast
throws a std :: bad_cast
exception
- if the target is a reference (ie
dynamic_cast<T&>(u)
), then if the check failsdynamic_cast
throws astd::bad_cast
exception - if the target is a pointer (ie
dynamic_cast<T*>(p)
), then if the checks failsdynamic_cast
returns a null pointer - finally, as a special case, if the target is
void*
, thendynamic_cast
instead returns the address of the full object
在这种情况下,您可以:
In this case, you may:
- 从
dynamic_cast< NonPlayableCharacter *>(_ allChar [0]) - > getNPCState()
切换到
dynamic_cast< NonPlayableCharacter& >(* _ allChar [0])。getNPCState()
,并让异常传播 - 测试cast的结果(
NonPlayableCharacter * test
此处)非空性
- switch from
dynamic_cast<NonPlayableCharacter*>(_allChar[0])->getNPCState()
todynamic_cast<NonPlayableCharacter&>(*_allChar[0]).getNPCState()
, and let the exception propagates - test the result of the cast (
NonPlayableCharacter* test
here) for non-nullity
这篇关于将指针从基本类型转换为子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!