Can I tell if a C++ virtual function is implemented

后端 未结 2 607
遇见更好的自我
遇见更好的自我 2020-12-12 05:12

I want to be able to tell at run-time if an instance of a class implements a virtual function. For example:

struct Base
{
    virtual void func(int x) { <         


        
2条回答
  •  抹茶落季
    2020-12-12 06:09

    What comes to my mind right now is to have a special "counter" variable, which will take its value based on the implementation and zero if the base-class function is called. I think that this sounds a bit obsolete style though and try to find a better solution in a moment. See the example for now:

        struct Base
        {
            virtual void func(int x, int& implemented) {implemented = 0;}
    
        };
    
        struct Deriv : Base
        {
            virtual void func(int x, int& implemented) override {implemented = 1;}
        };
    

    If the functions are void as in your example, you also can use "return codes" - just return implemented value back.

提交回复
热议问题