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

前端 未结 9 1418
不知归路
不知归路 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:21

    What about getting pointer to the base-class function on the first use and compare it with actual one

       class Base { virtual int foo(); }
       class Derived : public Base { virtual int foo(); }
    
       bool Xxx::checkOverride()
       {
           int (Base::*fpA)();
           int (Base::*fpb)();
    
           fpA = &Base::foo;
           fpB = &Derived::foo;
    
           return (fpA != fpB);
       }
    

提交回复
热议问题