Is there a function attribute (standard or compiler extension) that emits a warning if an overriding method doesn’t call the overridden method?
Like this:
class A
{
protected:
[[please_call]]
virtual void foo() { /* ... */ };
};
class B : public A
{
protected:
void foo() override {}
// WARNING: B::foo() does not call A::foo()
};
>Solution :
One way to do this is to use another function in the base class and the call the virtual function in that function. That would look like
class Base
{
public:
void doFoo() { Base::doFooImpl(); doFooImpl(); }
private:
virtual void doFooImpl() { std::cout << "Base\n"; }
};
class Derived : public Base
{
private:
void doFooImpl() override { std::cout << "Dervied\n"; }
};
int main()
{
Derived d;
d.doFoo();
}
output:
Base
Derived