473,748 Members | 10,889 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert from dynamic polymophism to static polymophism?

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
4 2516
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2980
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of strings'. I already know that no individual string will be longer than 50 characters. I just don't know before run time how many elements of the array will be needed. I have heard it is possible to dynamically allocate memory for a 2
2
5433
by: raxitsheth | last post by:
I am using array in my progrm... is there any tool /tips so that i can convert my program to Dynamic Array...so that I can Add more Element to Array.... Code is around 4000 line 'C' (not C++) program....
3
1312
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example below... --
15
3044
by: rwf_20 | last post by:
I just wanted to throw this up here in case anyone smarter than me has a suggestion/workaround: Problem: I have a classic producer/consumer system which accepts 'commands' from a socket and 'executes' them. Obviously, each different command (there are ~20 currently) has its own needed functionality. The dream goal here would be to remove all knowledge of the nature of the command at runtime. That is, I don't want ANY switch/cases...
5
6251
by: moondaddy | last post by:
I have a website that currently has all static htm pages and nothing will be dynamic for quite some time. This site is made up of a bunch of htm pages. is there any advantage or disadvantage of converting them to aspx pages? We are using VS 2005 and .net 2.0. Thanks. -- moondaddy@nospam.nospam
13
14616
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and Nicolai M. Josuttis. Chapter 14.) How to implement analogue of this technique via static polymorphism? Perhaps there is special design pattern for this purpose...
24
19088
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
1
7973
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
5
2585
by: bearophileHUGS | last post by:
I often use Python to write small programs, in the range of 50-500 lines of code. For example to process some bioinformatics data, perform some data munging, to apply a randomized optimization algorithm to solve a certain messy problem, and many different things. For that I often use several general modules that I have written, like implementation of certain data structures, and small general "utility" functions/classes, plus of course...
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.