By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

How to convert from dynamic polymophism to static polymophism?

P: n/a
Hi,

Some dynamic polymorphism programs can be converted to the equavalent
static polymorphism programs. I'm wondering if there are any generall
procedures that I can use to do this conversion.

Best wishes,
Peng

Nov 9 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Pe*******@gmail.com wrote:
Some dynamic polymorphism programs can be converted to the equavalent
static polymorphism programs. I'm wondering if there are any generall
procedures that I can use to do this conversion.


Sort of makes the question "Why would you want to do that?" pop
into my mind.

Also sort of makes the question "What do you mean by static
polymorphism?"
pop into my mind. Do you mean compile time as opposed to run time?

I'm thinking that, once you nail down a little more carefully what you
mean,
you will find you either don't want to do that, or there is no general
way of
doing that only case-specific ways.
Socks

Nov 9 '05 #2

P: n/a

Puppet_Sock wrote:
Pe*******@gmail.com wrote:
Some dynamic polymorphism programs can be converted to the equavalent
static polymorphism programs. I'm wondering if there are any generall
procedures that I can use to do this conversion.
Sort of makes the question "Why would you want to do that?" pop
into my mind.


Most design pattern examples are shown by declaring an abstract class
and using inheritance. But this kind of inheritance hierachy requires
virtual functions which have some overhead in terms of runtime. If
those pattern examples can be converted into using static polymorphism,
the virtual functions can be eliminated and the runtime should be
better.

Also sort of makes the question "What do you mean by static
polymorphism?"
pop into my mind. Do you mean compile time as opposed to run time?
What I mean is using type information to figure out what class(with
template) to use at compile time (used in STL).

I'm thinking that, once you nail down a little more carefully what you
mean,
you will find you either don't want to do that, or there is no general
way of
doing that only case-specific ways.
Socks


Nov 9 '05 #3

P: n/a
Pe*******@gmail.com wrote:
Puppet_Sock wrote:
Pe*******@gmail.com wrote:
Some dynamic polymorphism programs can be converted to the equavalent
static polymorphism programs. I'm wondering if there are any generall
procedures that I can use to do this conversion.


Sort of makes the question "Why would you want to do that?" pop
into my mind.


Most design pattern examples are shown by declaring an abstract class
and using inheritance. But this kind of inheritance hierachy requires
virtual functions which have some overhead in terms of runtime. If
those pattern examples can be converted into using static polymorphism,
the virtual functions can be eliminated and the runtime should be
better.


Maybe. Or not. If you are worried about the amount of overhead involved
in virtual functions, you are likely barking up the wrong tree to start
with.
Particularly if you have not measured the difference in specific cases
that are relevant to your application. In the bulk of cases, whatever
difference
there may be is trivial. And it is not necessarily the case that the
template
is faster.

But suppose the template is faster. And suppose that it makes your
code run, overall, faster by a couple milli-seconds, out of a total run
time of many hours. That's what I mean by "relevant to your
application."
If you have not measured the difference then it's silly to try to
figure out
any generic way of making the transformation. It's not generally
important,
and it's not really possible to do generically anyway.
Also sort of makes the question "What do you mean by static
polymorphism?"
pop into my mind. Do you mean compile time as opposed to run time?


What I mean is using type information to figure out what class(with
template) to use at compile time (used in STL).


Further, part of the point of run-time polymorphism is that you don't
want to have to figure out the type to put in the slot at compile time.
Consider, for example, the idea of using a factory. You want to be
able to substitute a child for a parent based on things that are not
known until run-time. Just as a trivial example, you might want to
base a choice on user input. That's a big part of why such things as
the factory pattern were invented.
Socks

Nov 9 '05 #4

P: n/a
Puppet_Sock wrote:
Pe*******@gmail.com wrote:
Puppet_Sock wrote:
Pe*******@gmail.com wrote:
> Some dynamic polymorphism programs can be converted to the equavalent
> static polymorphism programs. I'm wondering if there are any generall
> procedures that I can use to do this conversion.

Sort of makes the question "Why would you want to do that?" pop
into my mind.


Most design pattern examples are shown by declaring an abstract class
and using inheritance. But this kind of inheritance hierachy requires
virtual functions which have some overhead in terms of runtime. If
those pattern examples can be converted into using static polymorphism,
the virtual functions can be eliminated and the runtime should be
better.


Maybe. Or not. If you are worried about the amount of overhead involved
in virtual functions, you are likely barking up the wrong tree to start
with.
Particularly if you have not measured the difference in specific cases
that are relevant to your application. In the bulk of cases, whatever
difference
there may be is trivial. And it is not necessarily the case that the
template
is faster.

But suppose the template is faster. And suppose that it makes your
code run, overall, faster by a couple milli-seconds, out of a total run
time of many hours. That's what I mean by "relevant to your
application."
If you have not measured the difference then it's silly to try to
figure out
any generic way of making the transformation. It's not generally
important,
and it's not really possible to do generically anyway.


I agree with Puppet Sock about premature optimization. If, however, you
have shown with a profiler that the virtual function call is costing
you too much, you might switch over to static polymorphism to gain some
speed (probably trading some ease of use and code bloat). The FAQ gives
an example of this
(http://www.parashift.com/c++-faq-lit...tml#faq-33.11).
I will reiterate: do not PRESUME that the virtual function is too
expensive.

As for implementing it generally, check out the "Template Method"
pattern in _Design Patterns_. They don't define the conversion
procedure you're looking for, but that chapter could give you a start.

Cheers! --M

Nov 9 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.