怎么判断某个指针是由哪个类生成的?例如:class P
{
};class A : public P
{
};class B : public P
{
};P *pp1,*pp2;pp1 = new A();
pp2 = new B();在以后的代码里,怎么判断出 pp1 是由 A 生成的,而 pp2 是由 B 生成的?
{
};class A : public P
{
};class B : public P
{
};P *pp1,*pp2;pp1 = new A();
pp2 = new B();在以后的代码里,怎么判断出 pp1 是由 A 生成的,而 pp2 是由 B 生成的?
标准c++的typeid还没用过
顶
if (pp1->GetRuntimeClass()->IsDerivedFrom(RUNTIME_CLASS(A)))
{
TRACE(_T("Object is derived from A\n"));
}
不过前提是你的基类是从CObject继承下来的。
// compile with: /GR /EHsc
BOOL bIsSizingBar =
pBar->IsKindOf(RUNTIME_CLASS(CSizingControlBar));
判断pBar是不是CSizingControlBar类的指针!!