Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ways to detect whether a C++ virtual function has been redefined in a derived class

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).

like image 420
Chris Johnson Avatar asked Jan 19 '11 21:01

Chris Johnson


People also ask

When a virtual function is redefined by the derived?

(b) When a virtual function is redefined by the derived class, it is called as overriding.

Which of the following is true when virtual function is redefined in derived class?

Explanation: The virtual functions must be declared and defined in base class. The functions can be redefined in derived class. If redefined in derived class then it overrides the base class function definition.

What happens if a pure virtual function is not redefined in a derived class?

If the derived class does not define the pure virtual function; it will not throw any error but the derived class becomes an abstract class.

Can a virtual function be implemented in a derived class?

Yes, Its correct that a Derived class has to OVERRIDE the function which is Pure Virtual in the Parent Class.


1 Answers

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.

like image 158
Cheers and hth. - Alf Avatar answered Oct 01 '22 01:10

Cheers and hth. - Alf