unknown; wrote:
hi,
i've got the following classes:
class CImage {}
class CImageBlackWhite : public CImage {}
class CImageColor : public CImage {}
and i've got a global function
void function_name (CImage * image) {
if image is instace of CImageBlackWhite {
do something
}
CImageBlackWhite * cibw = dynamic_cast<CImageBlackWhite*>(image);
if(cibw) {
do something
}
else if image is instance of CImageColor {
do something else
}
CImageColor* cic = dynamic_cast<CImageColor*>(image);
if(cic) {
return;
}
However, Noah is right in his other post; this is a bad code smell.
There is often a better approach. The typical method is to add a
virtual method to CImage. CImageBlackWhite::foo (where foo is the new
virtual method) would then "do something" and CImageColor::foo would
"do something else". Then function_name would just call image->foo().
But unfortunately, this is not always possible to do. In particular, if
the CImage heirarchy is not under your control, you can't change it.
Evan