choosing an object to create

S

slitvinov

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

Phlip

slitvinov said:
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+++double+dispatch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top