jeudi 30 mars 2017

What exactly the specifier final does in C++? [duplicate]

This question already has an answer here:

I read about final specifier on cppreference, which is used to prevent virtual function from being overridden by derived classes. If we don't want to overload function, then why did we define the function to be virtual. I still don't have a full grasp of what exactly the specifier final does? How does it work? And when should I use it?

struct Base {
        virtual void func() const final;    // do not override
        virtual void g();
    };

    struct Derived : Base {
        void func() const;  // error: D::f attempts to override final B::f
        void g();       // OK
    };

Aucun commentaire:

Enregistrer un commentaire