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

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

问题描述

我在很多地方发现:

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

但是我总是想起的一件事是,为什么我们不能创建抽象类的实例?互联网上的许多地方都说创建实例是没有意义的,或者有人说应该将它们用作基类.但是为什么创建抽象类的实例会出错?

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?

推荐答案

您的void bar()=0;无效-=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.

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

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读取数据的细节与读取数据完全不同.通过Firewire(例如).

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天全站免登陆