473,883 Members | 2,819 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic polymorphism vs. Static polymorphism

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

Thanks!

May 30 '06 #1
13 14635
Krivenok Dmitry wrote:
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...

There are many concept that are called polymorphism or genericity:
http://en.wikipedia.org/wiki/Polymor...ter_science%29

Which ones do you mean (especially with "static polymorphism") exactly?
May 30 '06 #2
Ulrich Hobelmann wrote:
Krivenok Dmitry wrote:
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...

There are many concept that are called polymorphism or genericity:
http://en.wikipedia.org/wiki/Polymor...ter_science%29

Which ones do you mean (especially with "static polymorphism") exactly?


He means C++ templates, but the question is too broad to be answered
completely here. One could implement static polymorphism like this:

struct A
{
int Read(); // Does something
};

struct B
{
int Read(); // Does something else
};

template <class T>
class Reader
{
T& t_;
public:
Reader( T& t ) : t_( t ) {}

int DoSomething()
{
return t.Read();
}
};

This template is basically the same as using an abstract base class
with a virtual Read(), but it achieves the same effect without
virtuality, which can sometimes be useful.

Cheers! --M

May 30 '06 #3
Krivenok Dmitry wrote:
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...

Thanks!


What is the goal you are trying to accomplish?

If I had to guess, you are either trying to create some sort of compile
time container, or a run time container that supports heterogenous
types. In the former case, some variation on the type lists may be what
you want (see <url: http://www.ddj.com/dept/cpp/184403813>). In the
latter case, well, that isn't something that compile time polymorphism
can do for you. If the types you want to store have the same interface,
then use run time polymorphism. That is why it exists. If they don't,
then some form of discriminated union may do what you want.

--
Alan Johnson
May 30 '06 #4

mlimber wrote:
This template is basically the same as using an abstract base class
with a virtual Read(), but it achieves the same effect without
virtuality, which can sometimes be useful.


I have to debate that statement and say they are completely different.
Dynamic vs. Static polymorphism do completely different things
resulting in vastly different behavior. One simple example:

struct Abstract { virtual void f() = 0; }

struct A : Abstract { void f() {} };
struct B : Abstract { void f() {} };

void fun(Abstract * a) { a->f(); }

vs.

struct A { void f(); }
struct B { void f(); }

template <typename A>
void fun(A * a) { a->f(); }

Note how these two behave completely differently. One has a single
function that allows pointers of any of the three types to be passed in
and calls the appropriate implementation of f() for the input. The
other creates two completely different functions that accept completely
unrelated inputs; it only looks similar to the programmer. This is a
very important distinction; they are actually not the same at all.

May 30 '06 #5
Noah Roberts wrote:
mlimber wrote:
This template is basically the same as using an abstract base class
with a virtual Read(), but it achieves the same effect without
virtuality, which can sometimes be useful.


I have to debate that statement and say they are completely different.
Dynamic vs. Static polymorphism do completely different things
resulting in vastly different behavior. One simple example:

struct Abstract { virtual void f() = 0; }

struct A : Abstract { void f() {} };
struct B : Abstract { void f() {} };

void fun(Abstract * a) { a->f(); }

vs.

struct A { void f(); }
struct B { void f(); }

template <typename A>
void fun(A * a) { a->f(); }

Note how these two behave completely differently. One has a single
function that allows pointers of any of the three types to be passed in
and calls the appropriate implementation of f() for the input. The
other creates two completely different functions that accept completely
unrelated inputs; it only looks similar to the programmer. This is a
very important distinction; they are actually not the same at all.


I apologize for my imprecision. My point was that they are the same in
the sense that both invoke a function without knowing any further
implementation details. Certainly there are other differences between
these two types of polymorphism.

Cheers! --M

May 30 '06 #6

"Krivenok Dmitry" <di**@icebrai ns-soft.com> wrote in message
news:11******** *************@i 40g2000cwc.goog legroups.com...
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...

Thanks!


All elements of a container must be of the same type. If it is a pointer or
smart pointer to a common base class we have dynamic polymorphism. Static
polymorphism can be accomplished using a flag to determine the actual type:

struct clumsy
{
int flag;
union (...} other_stuff;
};

You can have an array of these but you must set the flag to the right value
for each type of data stored in the union and go through a switch or similar
logic structure every time you use it. If you want to add a new type to the
union you have to find all the logic structures and update them, so this
scheme is not very maintainable.

A variation on this theme is:

struct bummer
{
int flag;
};

struct radical
{
int flag;
double value;
};

radical r;
r.flag = 2;
r.value = 76.3;
bummer *p = (bummer*) &r;

Now you can have an array of bummer pointers, check the flag, and cast to
the actual type. This has all the problems of the union version but might
save a little memory in some cases I suppose.

Void pointers offer a sort of static polymorphism. I sometimes use them at C
interfaces where they are actually pointing to C++ classes. If you cast
carefully that can work.

Also see boost::any.

All and all when I look over these alternatives I certainly appreciate
dynamic polymorphism.

Cy
May 30 '06 #7
Cy Edmunds wrote:
"Krivenok Dmitry" <di**@icebrai ns-soft.com> wrote in message
All elements of a container must be of the same type. If it is a pointer or
smart pointer to a common base class we have dynamic polymorphism. Static
polymorphism can be accomplished using a flag to determine the actual type:

struct clumsy
{
int flag;
union (...} other_stuff;
};


Uh, I'm pretty sure this is not what most people mean by "static
polymorphism." How did you come to associate the term with this ugly
scheme?

The "static"/"dynamic" distinction wrt polymorphism, as with e.g.
typing, refers to compile-time vs. run-time. With static polymorphism,
the actual type of the object is known at compile-time. The usual
(only?) mechanism in C++ for this is templates.

Luke

May 31 '06 #8

"Luke Meyers" <n.***********@ gmail.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
Cy Edmunds wrote:
"Krivenok Dmitry" <di**@icebrai ns-soft.com> wrote in message
All elements of a container must be of the same type. If it is a pointer
or
smart pointer to a common base class we have dynamic polymorphism. Static
polymorphism can be accomplished using a flag to determine the actual
type:

struct clumsy
{
int flag;
union (...} other_stuff;
};


Uh, I'm pretty sure this is not what most people mean by "static
polymorphism." How did you come to associate the term with this ugly
scheme?

The "static"/"dynamic" distinction wrt polymorphism, as with e.g.
typing, refers to compile-time vs. run-time. With static polymorphism,
the actual type of the object is known at compile-time. The usual
(only?) mechanism in C++ for this is templates.

Luke


The original poster asked about heterogeneous containers. How would you do
that with templates?

Cy

May 31 '06 #9

Cy Edmunds wrote:
"Luke Meyers" <n.***********@ gmail.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
Cy Edmunds wrote:
"Krivenok Dmitry" <di**@icebrai ns-soft.com> wrote in message
All elements of a container must be of the same type. If it is a pointer
or
smart pointer to a common base class we have dynamic polymorphism. Static
polymorphism can be accomplished using a flag to determine the actual
type:

struct clumsy
{
int flag;
union (...} other_stuff;
};


Uh, I'm pretty sure this is not what most people mean by "static
polymorphism." How did you come to associate the term with this ugly
scheme?

The "static"/"dynamic" distinction wrt polymorphism, as with e.g.
typing, refers to compile-time vs. run-time. With static polymorphism,
the actual type of the object is known at compile-time. The usual
(only?) mechanism in C++ for this is templates.

Luke


The original poster asked about heterogeneous containers. How would you do
that with templates?

Check out the following example code:
http://code.axter.com/HeterogeneousContainer1.cpp
http://code.axter.com/HeterogeneousContainer2.cpp
http://code.axter.com/HeterogeneousContainer3.cpp

Each of the above files have different levels of complexity for
creating a heterogeneous container.
The basic idea is to create a wrapper class that acts like an interface
to the different types.
Although the types don't have to derive from the same object, they do
have to have a common method or common data to access.

----------------------------------------------------------------------------------------
David Maisonave
http://axter.com

Author of Axter's policy based smart pointers
(http://axter.com/smartptr)
Top ten member of C++ Expert Exchange:
http://www.experts-exchange.com/Cplusplus
----------------------------------------------------------------------------------------

May 31 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2195
by: Jason Tesser | last post by:
I work for at a college where I am one of 2 full-time developers and we are looking to program a new software package fro the campus. This is a huge project as it will include everything from registration to business office. We are considering useing Java or Python. I for one don't like Java because I feel the GUI is clunky. I also think that we could produce quality programs faster in Python.
5
2969
by: The Directive | last post by:
Does C++ support dynamic programming? I hope I'm using the correct term. I want to write code that can dynamically rewrite itself! I want to dynamically create functions and call them and etc. If not, are there any plans to add support for it in the future? What other popular programming languages support dynamic programming? --The Directive
2
3440
by: festiv | last post by:
Hi there, I want to learn how the compiler is implementing the dynamic binding. where can i read about this subject (the hole process). thanks.
4
4439
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their reuse for various template parameters. I wonder if there exist techniques for achieving a dynamic polymorphism using the generic programming. Is this possible? If yes, can anyone show me simple examples in C++
4
2527
by: PengYu.UT | last post by:
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
6
2929
by: MikeY | last post by:
Hi Everyone, Does anyone know where I can get my hands on a sample with source code of a simple dynamic button control in C# Windows form. I am looking for a sample that uses a class library that sets the properties send/passed from the main windows form. I'm having problems with the class library, the button control collection and my referencing it ie this.Control.Add(aControl);. Any and all help is appreciated. Thanks in advance.
15
3053
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...
2
1760
by: Mark P | last post by:
Still being relatively new to C++ I like to go back to review and rethink old designs to see where I could do things better. The issue I'm currently pondering is static vs. dynamic polymorphism, or equivalently in my case, templates vs abstract base classes. Let me be concrete. I have a class for performing a geometry operation which takes a collection of line segments and breaks them into minimal nonintersecting subsegments. I wrote...
0
9797
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
10762
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...
0
10422
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
7977
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
7136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6005
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4622
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
4228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3241
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.