I've got 3 classes:
class Super
{
virtual int getType() { return 1; }
}
class Special : public class Super
{
virtual int getType() { return 2; }
}
class SpecialSpecial : public class Special
{
virtual int getType() { return 3; }
}
And I've got a function which takes an std::vector<Super*>
as argument:
void handleClasses( std::vector<Super*> superVector )
{
foreach element in superVector //Pseudocode!!
{
if( element->getType() == 1 )
std::cout << "Thats a SuperClass" << std::endl;
else if( element->getType() == 2 )
std::cout << "Thats a SpecialClass" << std::endl;
else if( element->getType() == 3 )
std::cout << "Thats a SpecialSpecialClass" << std::endl;
}
}
There could be a User which inherits from class Special
:
class ParalellSpecial : public class Special
{
virtual int getType() { return 4; }
}
Now the function handleClasses
is not able to understand the new class with type 4, but it should be able to use the next superior class (in this case it is Special
).
How would someone implement such a thing? Is there a design pattern which allows to create an hierarchical order of classes and the next available superclass will be used as a fallback, if the current class could not be used (cause it is unknown)?
NOTE: The Type-Identifiers are just for demonstration-purposes. There surely are better methods to identify a classtype in C++ (RTTI).