C++0x 中的最终虚拟函数

发布于 2024-11-25 18:31:47 字数 166 浏览 0 评论 0原文

读到您可以在 C++0x 中拥有 最终虚拟函数 我是有点困惑。与一开始就省略两个修饰符有什么区别?

Reading that you can have final virtual functions in C++0x I am bit confused. What is the difference to just omitting both modifiers in the first place?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2024-12-02 18:31:47

区别在于不是使用它的基础,而是派生的。

class Base {
    virtual void foo() = 0;
};
class Derived : Base {
    void foo() {} 
    // Still virtual because it's virtual in base- no way to "un-virtual" it

    virtual void foo() final {} 
    // Now un-overridable.
};

不要将其视为防止覆盖,而是防止“更多”覆盖。

The difference occurs it's not the base that uses it, but the derived.

class Base {
    virtual void foo() = 0;
};
class Derived : Base {
    void foo() {} 
    // Still virtual because it's virtual in base- no way to "un-virtual" it

    virtual void foo() final {} 
    // Now un-overridable.
};

Think of it not as preventing overrides, but preventing "any more" overrides.

心清如水 2024-12-02 18:31:47

当我第一次在 C++ 中使用 final 关键字和 virtual 时,我也想知道同样的事情:

如果声明一个方法virtual使其可继承和可重写,并声明一个方法 最终 防止该方法被重写,声明一个方法两者不会形成矛盾吗?


我认为这个问题当前接受的答案很好,但我想根据我的情况进一步构建它发现调查这个。

考虑下面的类:

class A {
    public:
        void hide_me();
        virtual void override_me();
        virtual void cant_override_me() final;
};

要认识到的重要一点是这三个方法声明都是不同的并且意味着不同的东西。

第一个:

    void hide_me();

是非虚拟的,因此根据定义,不能被覆盖。

第三个:

    virtual void cant_override_me() final;

被声明为final,因此根据定义也不能被覆盖。

区别在于,由于 hide_me 是非虚拟的,因此覆盖它不适用,而您可以将 cant_override_me 视为符合条件 em> 被覆盖(因为它是虚拟但是它也具有覆盖禁用,因为final 修饰符。换句话说,重写不适用于未声明为 virtual 的方法,但它确实适用于 virtual 方法,只是如果它们也是 virtual 方法,则无法重写它们声明最终

现在考虑一个子类:

class B: public A {
    public:
        void hide_me(); // this hide's A's definition of "hide_me()"; this is not overriding.
        void override_me(); // implicit "virtual"
        //void cant_override_me(); // implicit "virtual"; compilation fails
};

可以为类B重新定义hide_me(),但这只是重载或隐藏,因此是函数名称。 B 仍然可以通过 A::hide_me() 访问 Ahide_me 方法,但其他人已经对声明为 BB 的引用,即:

B *my_b = new B();

必须通过以下方式访问 A 现在隐藏的 hide_me 定义my_b->A::hide_me()

不能B 中提供cant_override_me() 的重新定义。

作为一个完整的示例,这里对程序进行了轻微的重新定义,以帮助举例说明发生的情况:

#include <cstdio>    
class A {
    public:
        inline void hide_me() {
            printf("a hide_me\n");
        }
        virtual void override_me();
        virtual void cant_override_me() final;
};

class B: public A {
    public:
        inline void hide_me() {
            printf("b hide_me\n");
        }
        void override_me();
        inline void foo() {
            A::hide_me();
        }
        // can't override cant_override_me
};

void A::override_me() {
    printf("a override_me\n");
}

void A::cant_override_me() {
    printf("a cant_override_me\n");
}

void B::override_me() {
    printf("b override_me\n");
}

int main (int argc, char *argv[]) {
    A *a = new A();
    A *ab = new B();
    B *b = new B();

    a->hide_me();
    ab->hide_me();
    b->hide_me();
    b->A::hide_me();

    printf("---\n");

    a->override_me();
    ab->override_me();
    b->override_me();
    b->A::override_me();
}

程序输出

a hide_me
a hide_me
b hide_me
a hide_me
---
a override_me
b override_me
b override_me
a override_me

When I first came across the use of the final keyword in conjunction with virtual in C++, I was wondering the same thing:

If declaring a method virtual makes it inheritable and overridable, and declaring a method final prevents that method from being overriden, doesn't declaring a method both form a contradiction?

I think the current accepted answer to this question is good, but I wanted to build upon it a bit more based on what I found looking into this.

Consider the following class:

class A {
    public:
        void hide_me();
        virtual void override_me();
        virtual void cant_override_me() final;
};

The important thing to realize is that the three method declarations are all distinct and mean different things.

The first:

    void hide_me();

is non-virtual and thus, by definition, cannot be overridden.

The third:

    virtual void cant_override_me() final;

is declared final, and thus cannot be overridden, also by definition.

The difference is that since hide_me is non-virtual, overriding it is unapplicable, whereas you can think of cant_override_me as eligible to be overridden (because it is virtual,) but it also has overriding disabled due to the final modifier. In other words, overriding doesn't apply to methods that are not declared virtual, but it does apply to virtual methods, you just can't override them if they are also declared final.

Now consider a child class:

class B: public A {
    public:
        void hide_me(); // this hide's A's definition of "hide_me()"; this is not overriding.
        void override_me(); // implicit "virtual"
        //void cant_override_me(); // implicit "virtual"; compilation fails
};

You can redefine hide_me() for class B, but this is just overloading or hiding, hence the function name. B can still access A's hide_me method via A::hide_me(), but someone else who has a reference to B declared as B, i.e.:

B *my_b = new B();

must access A's now-hidden definition of hide_me via my_b->A::hide_me().

You cannot provide a redefinition of cant_override_me() in B.

As a full example, here is a slight redefinition of the program to help exemplify what's going on:

#include <cstdio>    
class A {
    public:
        inline void hide_me() {
            printf("a hide_me\n");
        }
        virtual void override_me();
        virtual void cant_override_me() final;
};

class B: public A {
    public:
        inline void hide_me() {
            printf("b hide_me\n");
        }
        void override_me();
        inline void foo() {
            A::hide_me();
        }
        // can't override cant_override_me
};

void A::override_me() {
    printf("a override_me\n");
}

void A::cant_override_me() {
    printf("a cant_override_me\n");
}

void B::override_me() {
    printf("b override_me\n");
}

int main (int argc, char *argv[]) {
    A *a = new A();
    A *ab = new B();
    B *b = new B();

    a->hide_me();
    ab->hide_me();
    b->hide_me();
    b->A::hide_me();

    printf("---\n");

    a->override_me();
    ab->override_me();
    b->override_me();
    b->A::override_me();
}

The program outputs

a hide_me
a hide_me
b hide_me
a hide_me
---
a override_me
b override_me
b override_me
a override_me
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文