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

人走茶凉 提交于 2019-12-02 12:29:51

问题


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) { <default behavior here> }
    virtual void func2(int x) { <default behavior here> }
};

struct Deriv : Base
{
    virtual void func(int x) override { <new behavior here> }
};

int main()
{
    Base *d = new Deriv;

    if(implements<Base::func(int)>(d)) // This should evaluate true
    {} 

    if(implements<Base::func2(int)>(d)) // This should evaluate false
    {}
}

I have seen this but it's dated and there might be a something that c++11/14 has to say now: Ways to detect whether a C++ virtual function has been redefined in a derived class


回答1:


The short answer is: No. And certainly not in a portable manner.

The longer answer: actually, the very goal of polymorphism is to make it indistinguishable to the user where the functionality comes from.

Even if the developer does not write code, the compiler might still generate a dedicated version of the function for the derived class (for better optimization). This would throw off even non-portable implementations that would inspect the virtual tables...

An alternative route, however, would be to throw off C++ automatic run-time polymorphism and instead provide an ad-hoc implementation. If for example you were to provide your own virtual table/pointer mechanism, then you would have more control:

struct VirtualTable {
    typedef void (*FuncType)(void*, int);
    typedef void (*Func2Type)(void*, int);

    FuncType func;
    Func2Type func2;
};

and you could check whether the function pointer is, or is not, equal to the default.




回答2:


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.



来源:https://stackoverflow.com/questions/33198672/can-i-tell-if-a-c-virtual-function-is-implemented

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!