class primaryobj{
//some features
}*obj1;
class secondaryobj: public primaryobj{
//some features
}*obj2;
class tertiaryobj: public secondaryobj{
//somefeatures
}*obj3;
somefunction(primaryobj *a, secondaryobj *b){
//some process
}
main(){
somefunction(obj1,obj2);//works fine
somefunction(obj1,obj3);//doesn't work expects a secondary obj.
}
Ok so I understand this is obviously a bit of an in theory question but why exactly can't the function
take a tertiaryobj instead of a secondary as tertiary is guaranteed to have all the features of a secondary.
Is there a way around this without rewriting out my whole function?
It works that way because that's how C++ ensures that types match. You can downcast your tertiaryobj to a secondaryobj when you call somefunction(). Or, you can make somefunction a template function.
Your code compiles and runs fine for me (Visual Studio 2008). And technically, this is not down casting as casting from tertiaryobj to secondaryobj is going up the inheritance chain. In this case, it's an up cast and you should be able to cast from an object to its parent object without problems.