为什么我们不能创建抽象类的实例? [英] Why can't we create an instance of an abstract class?

查看:51
本文介绍了为什么我们不能创建抽象类的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在很多地方发现:

  • 抽象类是应该用作基类的类.
  • 抽象类是至少具有一个纯虚函数的类.

但总让我想到的一件事是为什么我们不能创建一个抽象类的实例?网上很多地方说创建实例没有意义,或者说应该作为基类使用.但是为什么创建抽象类的实例会报错?

But one thing that always strikes my mind is why can't we create an instance of an abstract class? Many places on the Internet say there is no point in creating an instance, or some say that they are supposed to be used as base classes. But why is it an error to create an instance of an abstract class?

推荐答案

Your void bar()=0; is not valid -- =0 符号只能与虚函数一起使用.

Your void bar()=0; is not valid -- the =0 notation can only be used with virtual functions.

抽象类的全部意义在于它是抽象的——你定义了一个接口而不是一个实现.如果没有实现,实例化类不会产生有意义或有用的结果.如果实例化该类的对象确实/有意义,那么您根本不想首先使用抽象类.

The whole point of an abstract class is that it's abstract -- you've defined an interface but not an implementation. Without an implementation, instantiating the class wouldn't produce a meaningful or useful result. If it does/would make sense to instantiate objects of that class, then you simply don't want to use an abstract class in the first place.

例如,考虑设备驱动程序.我们可能有一个抽象存储设备的驱动程序.我们为该设备定义了一些功能,例如读取和写入数据.该抽象类为任何想要读/写数据的代码提供了使用从该抽象类派生的具体类的能力.

For example, consider device drivers. We might have a driver for an abstract storage device. We define some capabilities for that device, such as reading and writing data. That abstract class gives any code that wants to read/write data the ability to work with an concrete class that derives from that abstract class.

不过,我们不能仅仅实例化我们的抽象存储设备.相反,我们需要一个具体的对象,如拇指驱动器、磁盘驱动器等,来实际读取/写入.需要具体类是因为我们需要特定于实际设备的代码来执行我们在抽象基础中定义的命令.我们的抽象存储类只有读或写,但读或写,我们需要一个特定设备的驱动程序.一个人可能知道如何与 SATA 硬盘通信,而另一个人知道如何与 USB 拇指驱动器通信,第三个人知道如何读取或写入 SD 卡.然而,我们不能只是说我要创建一个抽象的存储设备",并在不定义将写入"命令转换为(例如) 正确的信号通过 SATA、USB、Firewire 等,将数据传输到真实驱动器上.

We can't just instantiate our abstract storage device though. Instead, we need a concrete object like a thumb drive, disk drive, etc., to actually read from/write to. The concrete class is needed because we need code specific to the actual device to carry out the commands we've defined in our abstract base. Our abstract storage class just has a read or write, but do the reading or writing, we need a driver for a specific device. One might know how to talk to a SATA hard drive, while another knows how to talk to a USB thumb drive and a third knows how to read from or write to an SD card. We can't, however, just say "I'm going to create an abstract storage device", and talk to it without defining the actual code that will translate a "write" command into (for example) the right signals going over SATA, USB, Firewire, etc., to get the data onto a real drive.

因此,尝试实例化我们的抽象类是没有意义的,也是不允许的.我们只使用抽象基类,这样系统的其余部分就可以统一处理所有设备.其余的代码并不关心信号彼此有何不同——它只是看到一堆磁盘驱动器,并且可以与所有这些驱动器一起工作,即使通过 USB 读取数据的细节与读取完全不同通过火线(例如).

As such, attempting to instantiate our abstract class makes no sense, and isn't allowed. We just use the abstract base class so the rest of the system can deal with all devices uniformly. The rest of the code doesn't care how the signals are different from each other -- it just sees a bunch of disk drives, and can work with all of them, even though the details of reading data over USB are completely different from reading over Firewire (for example).

这篇关于为什么我们不能创建抽象类的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆