どのように私はdamamcally形式の関数を呼び出すことができます:childA.function(childB)
その静的型は両方の親ですが?ポリモフィズム引数を使用して動的に関数を呼び出す方法
、より詳細で:
私は2つの分子の可能性を計算する必要がある物理学のプロジェクトを持っています。 しかし、私は2種類の分子、LCとColを持っていますが、それぞれのタイプには独自のパラメータとものがありますが、各分子の能力を動的に呼び出せるようにしたいと考えています。
ので、私はこの試みた:
#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);
}
をしかし、それはmol1の静的な型が分子であるため、呼び出された関数であることを意味し、関数は、スタティックにコールされていることが判明したとして、私は、4の結果が得られます。
virtual double potential(const Molecule * mol) {return 4;}
なく
virtual double potential(const LC * mol) {return 5;}
は、関数DYNAを呼び出すことがとにかくありそれにtypeidを使用しないでmically(OOPデザインで)?
フル答え:
#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);
}
:
ピーターのアドバイスと調査の後、これは完全な解決策は、ちょうどこのように仮想内の別の仮想を呼び出すことで、古典的な二重派遣の問題であることが判明
これは実際に必要に応じて3を返します。
あなたのソースコードがコンパイルされません。その犯人は、「分子」のメンバー関数です。 'Molecule'データメンバの名前を変更することもできます。多分あなたはそれらの純粋な仮想を作ることを忘れましたか?関数は静的に_呼び出されません_。 [MCVE](https://stackoverflow.com/help/mcve)を入力してください。 – Ron
訪問者パターンを調べます。 – Peter