MetaEngine::save() is re-implemented in DMetadata without to use virtual property.

Marcel Mathis maeseee at gmail.com
Fri Nov 13 16:41:18 GMT 2020


Hi Gilles

This is completely valid code if the virtual is missing. So I don't think
there is a compiler error or warning. See the attached example.

I would advise to use the compiler flag -Werror=suggest-override. So you
can guarantee that all overridden functions are at least virtual.

Hope to help you with this information.

Cheers
Marcel

On Fri, Nov 13, 2020 at 4:16 PM Gilles Caulier <caulier.gilles at gmail.com>
wrote:

> A partial response can be found here :
>
>
> https://stackoverflow.com/questions/1853896/is-it-possible-to-override-a-non-virtual-method
>
> This is typically possible but highly not recommended and not portable
> everywhere... as i understand...
>
> Gilles
>
> Le ven. 13 nov. 2020 à 16:11, Gilles Caulier
>
> <caulier.gilles at gmail.com> a écrit :
> >
> > And it's the same for MetaEngine::applyChanges() ....
> >
> > Gilles
> >
> > Le ven. 13 nov. 2020 à 16:10, Gilles Caulier
> > <caulier.gilles at gmail.com> a écrit :
> > >
> > > Hi, all,
> > >
> > > I currently review all Clazy static analyzer reports, and i plan to
> > > merge DMetadata class to MetaEngine.
> > >
> > > While reviewing the code I discovered that MetadaEngine::save() is
> > > implemented into DMetadata without being declared as virtual.
> > >
> > > I'm surprised to not see a G++ warning about this code, which can
> > > introduce side effects... Look by yourself :
> > >
> > >
> https://invent.kde.org/graphics/digikam/-/blob/master/core/libs/metadataengine/engine/metaengine.h#L372
> > >
> > >
> https://invent.kde.org/graphics/digikam/-/blob/master/core/libs/metadataengine/dmetadata/dmetadata.h#L95
> > >
> > > How is it technically possible ?
> > >
> > > Best
> > >
> > > Gilles Caulier
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/digikam-devel/attachments/20201113/fa3e676e/attachment.htm>
-------------- next part --------------
#include <iostream>

class A
{
    public:
    virtual void foo() {
        std::cout << "A::foo" << std::endl;
    }
    void bar() {
        std::cout << "A::bar" << std::endl;
    }
};

class B : public A
{
    public:
    void foo() override {
        std::cout << "B::foo" << std::endl;
    }
    void bar() {
        std::cout << "B::bar" << std::endl;
    }
};

int main() {
    A* a = new A();
    B* b = new B();
    A* c = b;

    a->foo();
    b->foo();
    c->foo();

    a->bar();
    b->bar();
    c->bar();

    return 0;
}
-------------- next part --------------
A::foo
B::foo
B::foo
A::bar
B::bar
A::bar


More information about the Digikam-devel mailing list