If Shutdown () is a virtual method of the base class, that is, BaseClass :: ShutDown (), you must directly call iter-> ShutDown ();
Otherwise, if the method is not virtual, you should use dynamic_cast.
vector<ADlgcDev*>::iterator iter;
for (iter = myVector.rbegin(); iter != myVector.end(); iter++)
{
DerivedClassA* a = dynamic_cast<DerivedClassA*>( *iter ) ;
if ( a ) a->ShutDownA();
else
{
DerivedClassB* b = dynamic_cast<DerivedClassB*>(*iter);
if ( b ) b->ShutDownB();
}
myVector.pop_back();
}
Anyway, you are probably leaking memory if ShutDown () doesn't remove the object from itself (which is usually bad), or you keep duplicate pointers and delete them elsewhere, which is another risky idea.
source
share