[Uml-devel] dynamic_cast usage
Carsten Pfeiffer
carpdjih at mailbox.tu-berlin.de
Wed Dec 18 11:00:11 UTC 2002
-----BEGIN PGP SIGNED MESSAGE-----
Hiya,
I found many dynamic_casts in umbrello, while most of them should be
static_casts, I think.
I.e. there is not much point in doing
return dynamic_cast<StateWidgetData *>( m_pData ) -> getActivityList();
dynamic_cast performs a runtime type-check and returns a valid pointer
(StateWidgetData * here) IF the to be casted pointer is of that type or a
subtype thereof. Otherwise it returns a 0L-pointer.
So, if you know that m_pData is a StateWidgetData, then you should use
return static_cast<StateWidgetData *>( m_pData ) -> getActivityList();
because static_cast doesn't involve dynamic typechecking (i.e. it is a bit
faster).
You need to use dynamic_cast if you don't know if m_pData points to an object
of type StateWidgetData or SomeOtherWidgetData. I.e. if you have a common
baseclass but you need to call a method that is only in one of specific
subclasses.
Then you do:
StateWidgetData *swd = dynamic_cast<StateWidgetData *>( m_pData );
if ( swd )
return swd->getActivityList();
else
[...]
Another thing I just noticed: deleting a 0L-pointer is perfectly valid, i.e.
there is no need check for non-0L before deleting something, like
if ( foo )
delete foo;
Cheers
Carsten Pfeiffer
-----BEGIN PGP SIGNATURE-----
iQEVAwUBPgDFf6WgYMJuwmZtAQHO1QgAiISYBDrvfgbRHeeWdMltSW66ga0dDY51
Y/Gip14FNxZUBCgVFmVQ80LOaE8a5bxQvkoeZazz2sheT2Unc0tpFy5Yky1kbJ2c
aoj7cIEmEjosCulSdcdU4dyloAsUSEhA+on2xtUixAgK5CY+2RbFXvq9a1N2PjHL
Sx6ZfxUhLaJNK4vFlBkWYECPfNy1HxwRRa6bDP1VQGVUSBmbbApLZ5pq3AR+S1B7
vh6Gh4VGrnuEzbHUTKOTTAdUTMBE57eBXEuWeeY1rIs/oqZpFm8Db9jgSzlbhtwk
Huaf9hmjOMYmDFXJUL9Q5a4HEPRXh5mLQlIVcWuScyQtM2VWwhNKmw==
=za8l
-----END PGP SIGNATURE-----
More information about the umbrello-devel
mailing list