检测是否在派生类中重新定义了C ++虚函数的方法 [英] Ways to detect whether a C++ virtual function has been redefined in a derived class

查看:115
本文介绍了检测是否在派生类中重新定义了C ++虚函数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简要说明:从指向派生类实例的C ++基类指针中,如何在运行时确定是否使用非纯虚函数
我正在编写一个C ++库来创建一个C ++类库求解某些类的数学方程。该库提供了一个带有几个虚函数的等式类,这些库用户用作他们希望解决的特定方程的基类。库还提供了一个 Solver 类,它将方程* 作为构造函数参数。然后,用户按照以下代码编写代码:

  class MyEquation:public Equation 
{...}方程定义这里

int main()
{
MyEquation myEqn;
求解器求解器(& myEqn);
solver.Solve();
}

如果 / code>不在派生方程类中重新定义,可以省略由 Solver 对象运行的算法的某些计算上昂贵的部分。因此,我想知道,在 Solver 的构造函数中,哪些函数已被重新定义,而将在方程中运行默认实现 code>。




  • 我想让这个图书馆的用户透明,所以我不是解决方案,例如,用户在其派生方程的构造函数中设置一些标志,指定哪些函数已被重新定义。


  • 一个可能的解决方案是对方程式中的虚函数的默认实现设置私有标记在方程类中; Solver 类的构造函数然后可以清除此标志,运行虚函数,并检查标志值以查看 Equation 已被调用。我想避免这个,虽然,因为简单地设置标志每次执行虚函数都会使算法变慢(这些虚函数的执行对程序的运行时间有很大影响,默认实现简单地返回一个常数)。



解决方案



您需要将知识带到 Solver ,最好通过类型而不是运行时



一种(基于类型)的方式是通过 dynamic_cast 检查是否存在接口。 。



一个更好的方法是提供solve函数的重载,在你的情况下, Solver 构造函数。 p>

如果您提供了更具体的问题描述,可能会提供更好的建议。它提醒了有人(1)需要解决一些问题P的典型情况,(2)设想技术方法X作为P的解决方案,(3)发现X不剪切它,以及(4)询问如何使得X对于P的模糊描述起作用,或者甚至对于一些不相关的问题Q。原始问题P的细节通常表明比X更好的解决方案,以及使得X工作的问题与解决P无关。 >

In brief: From a C++ base-class pointer which points to an instance of a derived class, how can one determine at run-time whether a non-pure virtual function (with an implementation in the base class) has been re-implemented in the derived class?

The context: I am writing a C++ library to solve certain classes of mathematical equation. The library provides an Equation class with several virtual functions, which library users use as a base class for the particular equation they wish to solve. The library also provides a Solver class, which takes an Equation * as a constructor parameter. The user then writes code along the lines of:

class MyEquation : public Equation
{ ... } // equation definition here

int main()
{
  MyEquation myEqn;
  Solver solver(&myEqn);
  solver.Solve();
}

If certain combinations of the virtual functions in Equation are not redefined in the derived equation class, certain computationally expensive parts of the algorithm run by the Solver object can be omitted. I would therefore like to know, in the constructor of Solver, which functions have been redefined, and which will instead run the default implementation in Equation.

  • I would like to make this transparent to users of the library so I am not looking for a solution where, for example, the user sets some flags in the constructor of their derived equation specifying which functions have been redefined.

  • One possible solution is for the default implementations of the virtual functions in Equation to set a private flag in the Equation class; the constructor of the Solver class can then clear this flag, run the virtual function, and check the flag value to see whether the implementation in Equation has been called. I would like to avoid this though, because simply setting the flag every time the virtual function is executed slows the algorithm down a good deal (the execution of these virtual functions contributes significantly to the run time of the program, and the default implementations simply return a constant).

解决方案

You can't check for override of virtual function portably.

You need to bring the knowledge to the Solver, preferably via type as opposed to run-time flag.

One (type-based) way is to check for presence or absence of a interface, via dynamic_cast.

A probably better way is to provide overloads of solve function, in your case the Solver constructor.

Probably better advice could be given if you provided a more concrete description of the problem. It does remind of typical situation where someone (1) needs to solve some problem P, (2) envisions technical approach X as a solution to P, (3) discovers that X doesn't cut it, and (4) asks how to make X work for a vague description of P, or even for some unrelated problem Q. The details of original problem P will often suggest a much better solution than X, and the problems of making X work, irrelevant to solving P.

这篇关于检测是否在派生类中重新定义了C ++虚函数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆