473,883 Members | 1,660 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
13 14633
Krivenok Dmitry wrote:
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...

#include <iostream>
#include <utility>

using namespace std;
class Nil {};

template <class H, class T> int length(pair<H,T > x) {
return 1 + length(x.second );
}
int length(Nil x) { return 0; }

int main(int argc, char* argv[])
{
cout << length( make_pair(1,
make_pair(2.0,
make_pair((int[]){9,8,7},
make_pair("a string",
make_pair(true, Nil())))))) << endl;

return 0;
}

May 31 '06 #11

"Axter" <go****@axter.c om> wrote in message
news:11******** **************@ h76g2000cwa.goo glegroups.com.. .

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


These are all based on dynamic polymorphism:
class Coins
{
public:
virtual int GetValue()=0;
};

Any smart pointer can function as a template based wrapper for a polymorphic
type.Cy
Jun 1 '06 #12

"Greg Buchholz" <sl************ **@yahoo.com> wrote in message
news:11******** *************@i 39g2000cwa.goog legroups.com...
Krivenok Dmitry wrote:
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...

#include <iostream>
#include <utility>

using namespace std;
class Nil {};

template <class H, class T> int length(pair<H,T > x) {
return 1 + length(x.second );
}
int length(Nil x) { return 0; }

int main(int argc, char* argv[])
{
cout << length( make_pair(1,
make_pair(2.0,
make_pair((int[]){9,8,7},
make_pair("a string",
make_pair(true, Nil())))))) << endl;

return 0;
}


Very cool! Done any Lisp programming? hehe

Cy
Jun 1 '06 #13
Cy Edmunds wrote:
"Axter" <go****@axter.c om> wrote in message
news:11******** **************@ h76g2000cwa.goo glegroups.com.. .

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


These are all based on dynamic polymorphism:
class Coins
{
public:
virtual int GetValue()=0;
};

Any smart pointer can function as a template based wrapper for a polymorphic
type.Cy


The interface is based on dynamic polymorphism, but the target type is
static polymorphism.
The target types (Penny, Dime, ....) are not derived from any class,
and Coins is just an interface to the static type.
This method mixes both dynamic polymorphism an static polymorphism to
get a heterogeneous container.

And, yes you could use a smart pointer to do this with an interface
class like Coins and the static holder class like TargetClassHold er.
std::vector<sma rt_ptr<Coins> > heterogeneousCo ntainer;

heterogeneousCo ntainer.push_ba ck(TargetClassH older<Nickel>);

That would work too.

Jun 1 '06 #14

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
11123
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...
0
10734
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
10407
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...
0
9567
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...
0
7114
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
5794
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
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4606
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
4211
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.