473,382 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

choosing an object to create

Hi,

I think I cannot make I good design decision in the following
situation
I have a class Particle. And a derived class PolymerParticle

Now I would like to create an object to represent the interaction
between two particles. For different pairs it should be a different
kind of interaction.
for Particle - Particle --- Interaction
for Particle - PolymerParticle --- PolymerHydroInteraction
for PolymerParticle - PolymerParticle -- PolymerPolymerInteraction

PolymerHydroInteraction and PolymerPolymerInteraction are derived from
the Interaction class.

At some point I have two pointers Particle*. And my current solution is
to create a function
Interaction* CreateInteraction(Particle*, Particle*)
This function uses dynamic_cast to convert Particle pointers and then
creates and returns the correct type of interaction.

Is there a better way to do the same thing? Maybe I am missing
something about C++.

Jun 6 '06 #1
2 1464
slitvinov wrote:
At some point I have two pointers Particle*. And my current solution is
to create a function
Interaction* CreateInteraction(Particle*, Particle*)
This function uses dynamic_cast to convert Particle pointers and then
creates and returns the correct type of interaction.

Is there a better way to do the same thing? Maybe I am missing
something about C++.


You have discovered the "double-dispatch problem". Don't cast your way out
of it.

http://www.google.com/search?q=c%2B%2B+double+dispatch

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 6 '06 #2
Phlip wrote:
You have discovered the "double-dispatch problem". Don't cast your way out
of it.

http://www.google.com/search?q=c%2B%2B+double+dispatch


Thank you. The link is very helpful. I am not a computer scientist and
did not know about design patterns. I see I have a lot to learn.

Jun 6 '06 #3

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

Similar topics

1
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to...
3
by: takarimasu | last post by:
How can i create an input object (text area,select) at runtime ? B.
7
by: Phi! | last post by:
Hello everyone, any help with this would be appreciated as I have spent the past afternoon trying to sort it out. I have two questions based on the followiung information. I have two...
2
by: Mike | last post by:
Hi, There are some way to know which object (A) create an instance of object (B) in the constructor of object (B)? Thanks.
37
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
4
by: jm.suresh | last post by:
Hi, Is there any standard text format for storing data of object oriented nature. The text file should be readable. That is, Is there any better way than having to write out a file like this...
10
by: MiddleTommy | last post by:
I know this is possible in C++ with pointers but dont know how to do it in C#. Simplified Sample Code and Comments //create object A object A = new object(); //create object...
46
by: Phil Reynolds | last post by:
I have Access 2000 and 2003 on my development machine. My client only has Access 2000. When I develop for this client, I run Access 2000. However, my code requires that I have the Microsoft Word...
11
by: Mark B | last post by:
I want to display a pre-designed graphical 'performance badge' on certain webpages (round, about 2cm diameter) next to a salesperson's details. I have a function,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.