gpt4 book ai didi

c++ - 纯虚函数和覆盖函数 (c++)

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:34:38 26 4
gpt4 key购买 nike

我在互联网上寻找我的问题的答案,但找不到任何答案,所以我来了。

为派生自纯虚拟的函数指定覆盖是否正确:

class baseClass
{
public:
virtual void myFunction() = 0;
}

class derivedClass : public baseClass
{
public:
virtual void myFunction() override;
}

这是正确的吗?

我的第二个问题是:即使没有类继承我的派生类(它将是最终的),我是否必须在我的函数的派生类中指定虚拟?

非常感谢您的回答!

最佳答案

Is this correct?

Yes. Override 确保该函数是虚函数并覆盖基类中的虚函数。如果这不是真的,则程序格式错误(生成编译时错误)。

Do I have to specify virtual in the derivedClass for my function even though no class will inherite from my derived class (it will be final)?

不,你不知道。但即使你离开虚拟说明符,它仍然是虚拟的。因为它已经在你的 BaseClass 中声明为 virtual。

If some member function vf is declared as virtual in a class Base, and some class Derived, which is derived, directly or indirectly, from Base, has a declaration for member function with the same

  • name
  • parameter type list (but not the return type)
  • cv-qualifiers
  • ref-qualifiers

Then this function in the class Derived is also virtual (whether or not the keyword virtual is used in its declaration) and overrides Base::vf (whether or not the word override is used in its declaration). Base::vf does not need to be visible (can be declared private, or inherited using private inheritance) to be overridden.

关于c++ - 纯虚函数和覆盖函数 (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22957981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com