Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

c++ - Difference between final class and override final

The question is pretty simple, what's the difference in using final like this,

class Base {
public:
    Base() {}
    virtual ~Base() {}

    virtual void Initialize() {}
};

class Derived final : public Base {
public:
    Derived() {}
    ~Derived() {}

    virtual void Initialize() override {}
};

And using override final like this,

class Base {
public:
    Base() {}
    virtual ~Base() {}

    virtual void Initialize() {}
};

class Derived : public Base {
public:
    Derived() {}
    ~Derived() {}

    virtual void Initialize() override final {}
};
question from:https://stackoverflow.com/questions/65873079/difference-between-final-class-and-override-final

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The first case locks the class. The following would cause an error:

class Derived2 : public Derived{};

The second forbids any class that inherits from it to redefine the finalmethod.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...