我有两节课:
/*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
or时返回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)
" "根据orDYNAMIC_DOWNCAST
返回NULL
or 。not NULL
class CEndSystem
class CSwitch
为什么“ 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;
}
并且在保存文档时会在序列化中调用这些函数。