【发布时间】:2011-02-07 04:49:42
【问题描述】:
我有一个无法修改的结构 simple_instr。我这样创建了一个派生类型:
struct ComplexInstruction : simple_instr
{
ComplexInstruction(const simple_instr& simple) : simple_instr(simple)
{
}
bool isHead;
bool isTail;
bool isPreHeader;
};
我希望能够判断 simple_instr 的实例是否实际上是 ComplexInstruction。我像这样创建 ComplexInstructions:
ComplexInstruction comInstr = *current; // current is a pointer to a simple_instr
ComplexInstruction* cInstr = &comInstr;
我尝试使用
ComplexInstruction* cInstr = static_cast<ComplexInstruction*>(current);
并检查它是否等于 null,但问题是强制转换总是成功,并且 cInstr 永远不会等于 null。
这样做的正确方法是什么?
【问题讨论】:
-
基类是多态的吗? (即,它有没有虚成员函数?)
-
不,不幸的是
标签: c++ inheritance casting