How can I dinamcally call function of the form:childA.function(childB)
while their static types are both of the parents?
and with more details:
I have a physics project where I need to calculate the potential of 2 molecules. But I have 2 types of molecules, LC and Col, and each type has its own parameters and stuff, however I want to be able to call the potential of each molecule dynamically.
so I tried this:
#include <iostream>
#include <typeinfo>
#include <stdio.h>
using namespace std;
class Col;
class LC;
class Molecule
{
public:
/// more data and functions should be here regarding the molecule
double someBulshit;
virtual double potential(const Molecule * mol){}
virtual double potential(const Col * mol){}
virtual double potential(const LC * mol){}
};
class LC : public Molecule
{
public:
/// more data and functions should be here regarding the LC molecule
virtual double potential(const Molecule * mol) {return 1;}
virtual double potential(const LC * mol) {return 2;}
virtual double potential(const Col * mol) {return 3;}
};
class Col : public Molecule
{
public:
/// more data and function should be here regarding the Col molecule
virtual double potential(const Molecule * mol) {return 4;}
virtual double potential(const LC * mol) {return 5;}
virtual double potential(const Col * mol) {return 6;}
};
int main(int argc, char* argv[])
{
Molecule * mol1 = new Col();
Molecule * mol2 = new LC();
double my_potential = mol1->potential(mol2);
printf ("%f",my_potential);
}
But I get result of 4, as it turns out that the function are called statically, which means that since the static type of mol1 is Molecule the function called is:
virtual double potential(const Molecule * mol) {return 4;}
and not
virtual double potential(const LC * mol) {return 5;}
Is there anyway to call the function dynamically (in OOP design) without the use of typeid in it?
full answer:
After investigation with Peter advice this turn out to be classical double-dispatch problem the full solution is just to call another virtual inside the virtual like this:
#include <iostream>
#include <typeinfo>
#include <stdio.h>
using namespace std;
class Col;
class LC;
class Molecule
{
public:
/// more data and functions should be here regarding the molecule
double someBulshit;
virtual double potential(const Molecule * mol) const = 0;
virtual double potential(const Col * mol) const = 0;
virtual double potential(const LC * mol) const = 0;
};
class LC : public Molecule
{
public:
/// more data and functions should be here regarding the LC molecule
virtual double potential(const Molecule * mol) const {return mol->potential(this);}
virtual double potential(const LC * mol) const {return 2;}
virtual double potential(const Col * mol) const {return 3;}
};
class Col : public Molecule
{
public:
/// more data and function should be here regarding the Col molecule
virtual double potential(const Molecule * mol) const {return mol->potential(this);}
virtual double potential(const LC * mol) const {return 5;}
virtual double potential(const Col * mol) const {return 6;}
};
int main(int argc, char* argv[])
{
Molecule * mol1 = new Col();
Molecule * mol2 = new LC();
double my_potential = mol1->potential(mol2);
printf ("%f",my_potential);
}
and this indeed return 3 as desired.