2

How to determine whether a pointer of base (B) class is (polymorphism-ly) override a certain virtual function of the base class?

class B{
    public: int aField=0;
    virtual void f(){};
};
class C : public B{
    public: virtual void f(){aField=5;};
};
class D: public B{};

int main() {
    B b;
    C c;
    D d;
    std::vector<B*> bs;
    bs.push_back(&b);
    bs.push_back(&c);
    bs.push_back(&d);
    for(int n=0;n<3;n++){
        //std::cout<<(bs[n]->f)==B::f<<std::endl;
        //should print "true false true"
    }
}

I tried to compare the address of function pointer bs[n]->f against B::f, but it is uncompilable.

Demo

I feel that this question might be duplicated, but I can't find (sorry).

cppBeginner
  • 1,114
  • 9
  • 27
  • What are you trying to do here? This seems like an [X Y problem](https://meta.stackexchange.com/q/66377/218012) – Justin Jul 19 '17 at 03:49
  • @Justin Mainly, because of curiosity. I would like to know if it is possible ... it may useful for some rare cases of optimization (if I would happen to need it). – cppBeginner Jul 19 '17 at 03:52
  • The question does not make much sense ; since `f` is virtual, `bs[n]->f` is always a virtual call – M.M Jul 19 '17 at 04:35
  • Do you mean to ask: how to tell if `*bs[n]` has dynamic type `B` or not? – M.M Jul 19 '17 at 04:36
  • @M.M No, I just want to check whether a single certain function `f` is overridden. e.g. `checkF(static_cast(new D()))` should return "It is not overridden". .... `D` can override other virtual function of `B`, but I don't care. – cppBeginner Jul 19 '17 at 04:58

1 Answers1

2

GCC has an extension that allows you to get the address of a virtual member function.

This can be used like so:

#include <vector>
#include <iostream>

class B{
    public: int aField=0;
    virtual void f(){};
};
class C : public B{
    public: virtual void f(){aField=5;};
};
class D: public B{};

int main() {
    B b;
    C c;
    D d;
    std::vector<B*> bs;
    bs.push_back(&b);
    bs.push_back(&c);
    bs.push_back(&d);
    for(int n=0;n<3;n++){
        // This might need to be: (void*) B{}.*&B::f == (void*) (...)
        std::cout << ((void*) &B::f == (void*)(bs[n]->*&B::f)) << '\n';
    }
}

Demo

You may find this QA to be interesting.

Naturally, this is nonstandard behavior. If you wanted similar behavior in standard C++, you might actually be looking for pure virtual functions:

class B{
    public: int aField=0;
    virtual void f() = 0;
};

Otherwise, you'd have to have some other mechanism to communicate, such as a bool return type on f().

Justin
  • 24,288
  • 12
  • 92
  • 142