S
Samee Zahur
Hello,
The other day I was rather shocked to find that I couldn't find
a good use for runtime polymorphism! Let me explain this a bit further
before you get shocked. Any function that I could previously write like
this:
void func1(Base& obj)
{
//...
obj.virmeth(); //Call virtual method
//...
}
Is now better written as this for most cases:
template <class T> void func1(T& obj)
{
//...
obj.virmeth(); //Call any method
//...
}
That way, I'll never incurr run-time overheads. Obviously, this
wouldn't
work if I'm trying to separate implementation from use, but this
question hit me so suddenly, that I really couldn't find a good answer
then. Now I figured out two situations where this isn't possible: when
return type must be polymorphic (or is that also used in automatic
template specialization?) ... or when catching exceptions. But I'd
still like to shake myself off this uneasy feeling by hearing a bit
more over which is more suitable when - and even in the cases I
mentioned, I'd like a little discussionW (exceptional cases etc.)
Samee
The other day I was rather shocked to find that I couldn't find
a good use for runtime polymorphism! Let me explain this a bit further
before you get shocked. Any function that I could previously write like
this:
void func1(Base& obj)
{
//...
obj.virmeth(); //Call virtual method
//...
}
Is now better written as this for most cases:
template <class T> void func1(T& obj)
{
//...
obj.virmeth(); //Call any method
//...
}
That way, I'll never incurr run-time overheads. Obviously, this
wouldn't
work if I'm trying to separate implementation from use, but this
question hit me so suddenly, that I really couldn't find a good answer
then. Now I figured out two situations where this isn't possible: when
return type must be polymorphic (or is that also used in automatic
template specialization?) ... or when catching exceptions. But I'd
still like to shake myself off this uneasy feeling by hearing a bit
more over which is more suitable when - and even in the cases I
mentioned, I'd like a little discussionW (exceptional cases etc.)
Samee