我是否还需要在子类方法上指定 virtual ?

发布于 2024-09-08 04:18:17 字数 463 浏览 8 评论 0原文

以前可能有人问过这个问题,但我找不到类似的问题。

考虑以下类层次结构:

class BritneySpears
{
  public:

    virtual ~BritneySpears();
};

class Daughter1 : public BritneySpears
{
  public:

    virtual ~Daughter1(); // Virtual specifier
};

class Daughter2 : public BritneySpears
{
  public:

    ~Daughter2(); // No virtual specifier
};

Daughter1Daughter2 类之间有区别吗?

在子类析构函数/方法上指定/不指定virtual会产生什么后果?

This has probably been asked before on SO, but I was unable to find a similar question.

Consider the following class hierarchy:

class BritneySpears
{
  public:

    virtual ~BritneySpears();
};

class Daughter1 : public BritneySpears
{
  public:

    virtual ~Daughter1(); // Virtual specifier
};

class Daughter2 : public BritneySpears
{
  public:

    ~Daughter2(); // No virtual specifier
};

Is there a difference between Daughter1 and Daughter2 classes ?

What are the consequences of specifying/not specifying virtual on a sub-class destructor/method ?

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

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

发布评论

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

评论(3

删除→记忆 2024-09-15 04:18:17

不,从技术上讲,您不需要指定虚拟。如果基本成员函数是虚拟的,那么 C++ 将自动将匹配的重写成员函数设为虚拟。

但是,您应该将它们标记为override,这确保它是virtual,并且它覆盖基类中的成员函数。毕竟,该方法是虚拟的,它使您的代码更加清晰,更容易被其他开发人员遵循。


注意:在 C++11 之前,您可以将重写成员函数设为 virtual,因为 override 尚不可用。

No you technically do not need to specify virtual. If the base member function is virtual then C++ will automatically make the matching override member function virtual.

However you should be marking them override, which ensures that it's virtual, and also that it overrides a member function in the base class. The method is virtual after all, and it makes your code much clearer and easier to follow by other developers.


Note: prior to C++11, you could make the overriding member function just virtual, since override isn't available yet.

芸娘子的小脾气 2024-09-15 04:18:17

您不需要它,但标记它可能会让您的代码更清晰。

注意:如果您的基类有一个虚拟
析构函数,那么你的析构函数是
自动虚拟。你可能需要
other 的显式析构函数
有原因,但没必要
重新声明一个析构函数只是为了使
确保它是虚拟。无论是否
你用virtual声明它
关键字,声明它时不带
virtual 关键字,或者不声明它
无论如何,它仍然是虚拟

- C++ 常见问题解答 - 我的析构函数何时应该是虚拟的?

You do not need it, but marking it so may make your code clearer.

Note: if your base class has a virtual
destructor, then your destructor is
automatically virtual. You might need
an explicit destructor for other
reasons, but there's no need to
redeclare a destructor simply to make
sure it is virtual. No matter whether
you declare it with the virtual
keyword, declare it without the
virtual keyword, or don't declare it
at all, it's still virtual.

- C++ FAQ - When should my destructor be virtual?

暖伴 2024-09-15 04:18:17

无论您是否在子类中指定它,都会在派生方法重写中自动选择 Virtual。

主要后果是,如果没有在子类中指定 virtual,则很难从子类定义中看出该方法实际上是 virtual 的。因此,我总是在父类和子类中指定 virtual。

Virtual is automatically picked up on derived method overrides regardless of whether you specify it in the child class.

The main consequence is that without specifying virtual in the child it's harder to see from the child class definition that the method is in fact virtual. For this reason I always specify virtual in both parent and child classes.

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