C++纯虚函数有函数体

发布于 2024-10-27 20:30:46 字数 83 浏览 1 评论 0原文

纯虚函数(当我们设置= 0时)也可以有一个函数体。

如果纯虚函数根本不会被调用,那么为它们提供函数体有什么用呢?

Pure virtual functions (when we set = 0) can also have a function body.

What is the use to provide a function body for pure virtual functions, if they are not going to be called at all?

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

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

发布评论

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

评论(3

深海里的那抹蓝 2024-11-03 20:30:46

您认为无法调用纯虚函数的假设是绝对错误的。当一个函数被声明为纯虚函数时,它仅仅意味着该函数无法通过虚拟调度机制动态调用。然而,这个完全相同的函数可以轻松地静态非虚拟直接调用(无需虚拟调度)。

在C++语言中,当在调用中使用函数的限定名称时,即当在调用中指定的函数名称具有::<;时,执行对虚函数的非虚拟调用。函数名>形式。

例如

struct S 
{
  virtual void foo() = 0;
};

void S::foo() 
{
  // body for pure virtual function `S::foo`
}

struct D : S 
{
  void foo() 
  {
    S::foo();       
    // Non-virtual call to `S::foo` from derived class

    this->S::foo(); 
    // Alternative syntax to perform the same non-virtual call 
    // to `S::foo` from derived class
  }
};

int main() 
{
  D d;

  d.S::foo(); 
  // Another non-virtual call to `S::foo`
}

Your assumption that pure virtual function cannot be called is absolutely incorrect. When a function is declared pure virtual, it simply means that this function cannot get called dynamically, through a virtual dispatch mechanism. Yet, this very same function can easily be called statically, non-virtually, directly (without virtual dispatch).

In C++ language a non-virtual call to a virtual function is performed when a qualified name of the function is used in the call, i.e. when the function name specified in the call has the <class name>::<function name> form.

For example

struct S 
{
  virtual void foo() = 0;
};

void S::foo() 
{
  // body for pure virtual function `S::foo`
}

struct D : S 
{
  void foo() 
  {
    S::foo();       
    // Non-virtual call to `S::foo` from derived class

    this->S::foo(); 
    // Alternative syntax to perform the same non-virtual call 
    // to `S::foo` from derived class
  }
};

int main() 
{
  D d;

  d.S::foo(); 
  // Another non-virtual call to `S::foo`
}
蓦然回首 2024-11-03 20:30:46

“Effective C++”Meyers 提到了
纯虚函数的原因
有一个主体:派生类
实现这个纯虚函数
可以将此实现称为 smwhere
在他们的代码中。如果代码的一部分
两个不同的派生类是
类似,那么移动它是有意义的
在层次结构中向上,即使
函数应该是纯虚函数。

请参阅此处

"Effective C++" Meyers mentions a
reason for a pure virtual function to
have a body: Derived classes that
implement this pure virtual function
may call this implementation smwhere
in their code. If part of the code of
two different derived classes is
similar then it makes sense to move it
up in the hierarchy, even if the
function should be pure virtual.

see here.

日裸衫吸 2024-11-03 20:30:46

对于大多数纯虚函数,你是对的。然而,对于纯虚析构函数来说,定义相应的析构函数实现实际上很重要:

  • “纯虚”就是要求派生类实现其析构函数。
  • 您的基类析构函数实现是为了使派生类析构函数随后可以成功“链接”。

For most pure virtual functions, you'd be right. However, for a pure virtual destructor, it's actually important to define a corresponding destructor implementation:

  • The "pure virtual" is to require derived classes to implement their destructor.
  • Your base class destructor implementation is so that the derived class destructors can successfully "chain up" afterwards.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文