473,655 Members | 3,105 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 14608
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
2162
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
2956
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
3412
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
4421
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
2510
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
2907
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
3035
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
1744
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
8380
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
8710
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
7310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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
5627
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
4150
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
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2721
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
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.