【发布时间】:2012-12-28 10:41:12
【问题描述】:
我有两个班级:
/*Switch.h*/
class CSwitch : public CDeviceEntity {}
/*EndSystem.h*/
class CEndSystem : public CDeviceEntity {}
但是当我使用时:
CDeviceEntity* dev = NULL;
dev = topo->headList[i]->node;
if ( DYNAMIC_DOWNCAST( CEndSystem, dev ) != NULL ) {}
“DYNAMIC_DOWNCAST”总是返回not NULL,而dev 是class CEndSystem 或class CSwitch。
如果使用:
/*Switch.h*/
class CSwitch : public CDeviceEntity { DECLARE_DYNAMIC(CSwitch) }
and
/*Switch.cpp*/
IMPLEMENT_DYNAMIC(CSwitch, CDeviceEntity)
/*EndSystem.h*/
class CEndSystem : public CDeviceEntity { DECLARE_DYNAMIC(CEndSystem) }
and
/*EndSystem.cpp*/
IMPLEMENT_DYNAMIC(CEndSystem, CDeviceEntity)
“DYNAMIC_DOWNCAST”根据class CEndSystem或class CSwitch返回NULL或not NULL。
为什么“DECLARE_DYNAMIC”和“IMPLEMENT_DYNAMIC”对“DYNAMIC_DOWNCAST”是必不可少的?
/*Algorithm.h*/
static int getESNum();
/*Algorithm.cpp*/
int CAlgorithm::getESNum()
{
int count = 0;
CDeviceEntity* dev = NULL;
for (int i = 0; i < topo->nodeNum; i++)
{
dev = topo->headList[i]->node;
if ( DYNAMIC_DOWNCAST( CEndSystem, dev ) != NULL )
{
count++;
}
}
return count;
}
/*Algorithm.h*/
static int getSWNum();
/*Algorithm.cpp*/
int CAlgorithm::getSWNum()
{
int count = 0;
CDeviceEntity* dev = NULL;
for (int i = 0; i < topo->nodeNum; i++)
{
dev = topo->headList[i]->node;
if ( DYNAMIC_DOWNCAST(CSwitch, dev) != NULL )
{
count++;
}
}
return count;
}
并且在保存文档时会以序列化方式调用函数。
【问题讨论】:
-
DYNAMIC_DOWNCAST等的声明在哪里? -
您能否至少提一下
DYNAMIC_CAST、IMPLEMENT_DYNAMIC和DECLARE_DYNAMIC的来源?我从来没有遇到过。 -
@KonradRudolph,我已经编辑了帖子。
-
@Rubby 但是你没有给我们我要求的东西,只是添加了不相关的 cmets。
-
@KonradRudolph 谢谢,我已经添加了代码...