R
richardclay09
Please take a look at this method:
template<class C> void f(C* ptrAny) {
Fruit* ptrFruit = dynamic_cast<Fruit*>(ptrAny);
if(ptrFruit) {
// do something specific to fruits
}
// Carry on using ptrAny whatever it is
}
So I have a template function, but for Fruits I also want to do a
certain specific thing.
All is well and good until I happen to call F with a non-polymorphic
type. Then it barfs with a compile error.
So how can I do this where the templated C type can be polymorphic or
not? NB I don't want to overload f with a non-template like this:
void f(Fruit* ptrFruit) {...}
....because I have several templated functions like f, and also because
how would the compiler know which one to call for a Fruit, the
Fruit-specific one or the templated one instantiated for a Fruit?
Thanks for any help.
template<class C> void f(C* ptrAny) {
Fruit* ptrFruit = dynamic_cast<Fruit*>(ptrAny);
if(ptrFruit) {
// do something specific to fruits
}
// Carry on using ptrAny whatever it is
}
So I have a template function, but for Fruits I also want to do a
certain specific thing.
All is well and good until I happen to call F with a non-polymorphic
type. Then it barfs with a compile error.
So how can I do this where the templated C type can be polymorphic or
not? NB I don't want to overload f with a non-template like this:
void f(Fruit* ptrFruit) {...}
....because I have several templated functions like f, and also because
how would the compiler know which one to call for a Fruit, the
Fruit-specific one or the templated one instantiated for a Fruit?
Thanks for any help.