为什么定义纯虚函数有意义?

Why does it make sense to give definition for a pure virtual function?(为什么定义纯虚函数有意义?)
本文介绍了为什么定义纯虚函数有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scott 在《Effective C++, 3rd Edition, pg.》上说.43 要创建一个抽象类,我们只需要给它一个纯虚析构函数:

Scott said on Effective C++, 3rd Edition, pg. 43 that to make an abstract class, we just need to give it a pure virtual destructor:

class AWOV {                  // AWOV = "Abstract w/o Virtuals"
public:
  virtual ~AWOV() = 0;        // declare pure virtual destructor
};

然后,他继续说有一个转折:我们必须为纯虚析构函数提供一个定义:

Then, he went on said that there is one twist: we must provide a definition for the pure virtual destructor:

AWOV::~AWOW() {}              // definition of pure virtual dtor

我的问题是,通过指定 = 0,对于纯虚函数,我们是说该函数不能对声明此纯虚函数的类有任何定义.

My question is, by specifiying = 0, for pure virtual functions, we are saying that the function cannot have any definition for the class where this pure virtual function is declared.

为什么在这里为纯虚析构函数提供定义(即使是空的)也可以?

Why is it OK to provide a definition (even it is empty) for the pure virtual destructor here?

推荐答案

我们是说函数不能对声明这个纯虚函数的类有任何定义."

"we are saying that the function cannot have any definition for the class where this pure virtual function is declared."

这不是纯虚拟的意思.纯虚拟只意味着包含的类不能被实例化(是抽象的),所以它必须被子类化,并且子类必须覆盖该方法.例如,

That's not what pure virtual means. Pure virtual only means that the containing class cannot be instantiated (is abstract), so it has to be subclassed, and subclasses must override the method. E.g.,

struct A {
    virtual ~A() = 0;
};

A::~A() {}

struct B : A {};

int main()
{
    A a;  // error
    B b;  // ok
}

这里,B 析构函数是隐式定义的.如果它是另一种纯虚拟方法,则必须显式覆盖它:

Here, the B destructor is implicitly defined. If it was another method that is pure virtual, you'd have to explicitly override it:

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

void A::foo() {}

struct B : A {};

int main()
{
    B b;  // error
}

当基类必须是抽象的但仍提供一些默认行为时,需要为纯虚方法提供定义.

Providing a definition for a pure virtual method is desirable when the base class must be abstract but still provide some default behavior.

在析构函数的特定情况下,它必须提供,因为它会被自动调用 当子类实例被销毁时.尝试使用没有定义的纯虚析构函数实例化类的子类的程序将不会通过链接器.

In the specific case of a destructor, it has to be provided because it will be called automatically when subclass instances are destroyed. A program that tries to instantiate a subclass of a class with a pure virtual destructor without a definition will not pass the linker.

这篇关于为什么定义纯虚函数有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Polymorphic copy-constructor with type conversion(具有类型转换的多态复制构造函数)
Cast to a Child(铸造给孩子)
How to use virtual functions to achieve a polymorphic behavior in C++?(如何使用虚函数在 C++ 中实现多态行为?)
Polymorphism is not working with function return values of same data type (Base and Inherited class)(多态性不适用于相同数据类型(基类和继承类)的函数返回值)
Size of class with virtual function adds extra 4 bytes(具有虚函数的类的大小增加了额外的 4 个字节)
Right design pattern to deal with polymorphic collections of objects(正确的设计模式来处理对象的多态集合)