How to convert from dynamic polymophism to static polymophism?

P

PengYu.UT

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
 
P

Puppet_Sock

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
 
P

PengYu.UT

Puppet_Sock said:
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).
 
P

Puppet_Sock

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.
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
 
M

mlimber

Puppet_Sock said:
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-lite/pointers-to-members.html#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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,184
Latest member
ZNOChrista

Latest Threads

Top