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

前端 未结 9 1447
不知归路
不知归路 2020-12-01 14:10

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 functio

9条回答
  •  半阙折子戏
    2020-12-01 14:36

    And what if you didn't use virtual methods ?

    At this point I am think of a template-base solution. It is possible (albeit not so easy) to use templates to detect whether a given class has a method with a certain signature, or not. Once identified, you can switch, at compile-time, between the lightweight scheme and the heavy scheme.

    Note that I do not suggest that all the code be templated, the templated code will only cover the Template (Design Pattern) part of the solution, and the heavy-lifting can be done with regular functions to cut on dependencies.

    Also, this can be completely transparent for the clients, providing you do not alter the signature of the methods.

提交回复
热议问题