如何使用多态参数动态调用函数

时间:2017-07-03 10:27:04

标签: c++ oop polymorphism double-dispatch

我怎样才能将形式的函数称为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;}

是否有动态调用函数(在OOP设计中)而不使用typeid?

完整答案:

  

经过与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.

1 个答案:

答案 0 :(得分:1)

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

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

#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上调用我也添加了缺少的虚拟析构函数,因为多态类应该总是有它。 / p>

或者,您可以尝试使用模板代码实现此功能,并避免首先使用运行时多态性(即永远不要保留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);
}