0

我怎样才能动态地调用表单的函数:childA.function(childB)而它们的静态类型都是父母?

并提供更多详细信息:

我有一个物理项目,我需要计算 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);
}

但是我得到了 4 的结果,因为事实证明该函数是静态调用的,这意味着由于 mol1 的静态类型是 Molecule,所以调用的函数是:

virtual double potential(const Molecule * mol) {return 4;}

并不是

virtual double potential(const LC * mol) {return 5;}

无论如何在不使用 typeid 的情况下动态调用函数(在 OOP 设计中)?

完整答案:

在 Peter 的建议下进行调查后,这被证明是经典的双重调度问题,完整的解决方案就是在虚拟内部调用另一个虚拟,如下所示:

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

4

1 回答 1

1

你总是调用潜力(分子*),因为这是你传递的参数的类型。

如果你想动态地做它(即做出运行时决定),你将不得不做一个动态转换来决定你实际拥有的类型,然后调用正确的函数。例如,您可以仅在基类中使用基类型参数实现函数,检查实际类型,然后使用正确的类型调用重载函数。然后代码可能如下所示:

#include <iostream>
#include <typeinfo>
#include <stdio.h>

using namespace std;
class Col;
class LC;

class Molecule
{
  public:
  virtual ~Molecule() {}
  /// more data and functions should be here regarding the molecule
  double someBulshit;
  double potential(const Molecule * mol);
  virtual double potential(const Col * mol) = 0;
  virtual double potential(const LC * mol) = 0;
};

class LC : public Molecule
{
  public:
  virtual ~LC() {}
  /// more data and functions should be here regarding the LC molecule
  virtual double potential(const LC * mol) {return 2;}
  virtual double potential(const Col * mol) {return 3;}
};
class Col : public Molecule
{
  public:
  virtual ~Col() {}
  /// more data and function should be here regarding the Col molecule
  virtual double potential(const LC * mol) {return 5;}
  virtual double potential(const Col * mol) {return 6;}
};

double Molecule::potential(const Molecule * mol) {
  const Col *mol_as_Col = dynamic_cast<const Col*>(mol);
  const LC *mol_as_LC = dynamic_cast<const LC*>(mol);
  if(mol_as_Col) {
    return potential(mol_as_Col);
  }
  else if(mol_as_LC) {
    return potential(mol_as_LC);
  }
  else {
    throw;
  }
}


int main(int argc, char* argv[])
{
  Molecule * mol1 = new Col();
  Molecule * mol2 = new LC();

  double my_potential = mol1->potential(mol2);
  printf ("%f",my_potential);
}

结果将是 5 而不是您在问题中写的 6,因为您输入了 LC 类型并在 Col 上进行了调用。我还添加了缺少的虚拟析构函数,因为多态类应该始终拥有它。

或者,您可以尝试使用模板代码来实现这一点,并首先避免使用运行时多态性(即永远不要持有 Molecule* 类型的指针)。如果您有兴趣,我可以举一个例子,但由于您特别要求动态解决方案,它似乎不是这个问题的答案。

编辑:下面是一个模板演示。没有上下文就没有真正的意义。我添加了一个函数 printPotential(),它演示了如何编写其余代码:

#include <iostream>
#include <typeinfo>
#include <stdio.h>

using namespace std;

template<typename MOLECULE1, typename MOLECULE2>
void printPotential(MOLECULE1 *mol1, MOLECULE2 *mol2) {
  double my_potential = mol1->potential(mol2);
  printf("%f",my_potential);
}

class Col;

class LC
{
  public:
  /// more data and functions should be here regarding the LC molecule
  double potential(const LC * mol) {return 2;}
  double potential(const Col * mol) {return 3;}
};
class Col
{
  public:
  /// more data and function should be here regarding the Col molecule
  double potential(const LC * mol) {return 5;}
  double potential(const Col * mol) {return 6;}
};


int main(int argc, char* argv[])
{
  Col *mol1 = new Col();
  LC *mol2 = new LC();

  printPotential(mol1, mol2);
}

实际上,在写这篇文章时,我认为可能有第三种(实际上是首选)方法:您需要找到一种抽象算法,如何仅使用可以是基类的一部分(或获得的公共信息)来计算两个分子之间的电位通过基类的虚函数调用)。在这种情况下,您可以有一个 potential() 实现(作为基类的成员函数,具有一个 Molecule* 类型的单个参数,或者作为一个非成员函数,具有两个 Molecule* 类型的参数)。如果我假设两个分子之间的电位是两个“绝对”电位之差(只是一个愚蠢的例子),它可能看起来像这样:

#include <iostream>
#include <typeinfo>
#include <stdio.h>

using namespace std;
class Col;
class LC;

class Molecule
{
  public:
  virtual ~Molecule() {}
  /// more data and functions should be here regarding the molecule
  double potential(const Molecule * mol) {
    return mol->getAbsolutePotential() - getAbsolutePotential();
  }
  virtual double getAbsolutePotential() const = 0;
};

class LC : public Molecule
{
  public:
  virtual ~LC() {}
  /// more data and functions should be here regarding the LC molecule
  double getAbsolutePotential() const {return 42.0;}
};
class Col : public Molecule
{
  public:
  virtual ~Col() {}
  /// more data and function should be here regarding the Col molecule
  double getAbsolutePotential() const {return 120.0;}
};


int main(int argc, char* argv[])
{
  Molecule * mol1 = new Col();
  Molecule * mol2 = new LC();

  double my_potential = mol1->potential(mol2);
  printf ("%f",my_potential);
}
于 2017-07-03T11:05:26.213 回答