473,890 Members | 1,636 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 2527
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
2992
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
5444
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
3054
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
6255
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
14636
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
19115
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
7984
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
2594
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
9816
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11218
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10910
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
10452
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
8009
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
6036
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4667
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
2
4260
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3268
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.