为什么我们不能创建抽象类的实例?

发布于 2024-10-19 13:14:00 字数 168 浏览 6 评论 0原文

我在很多地方发现:

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

但我经常想到的一件事是为什么我们不能创建抽象类的实例?互联网上很多地方都说创建实例没有意义,或者有些地方说它们应该用作基类。但为什么创建抽象类的实例会出错呢?

I found in many places that :

  • An Abstract Class is a class which is supposed to be used as a base class.
  • An Abstract Class is a class which has atleast one Pure Virtual Function.

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?

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

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

发布评论

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

评论(7

花桑 2024-10-26 13:14:00

您的 void bar()=0; 无效 - =0 表示法只能与虚函数一起使用。

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

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

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

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

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.

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.

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).

草莓味的萝莉 2024-10-26 13:14:00

抽象类代表一些不够具体而无法实例化的东西。例如,如果有人要求你制造一辆车怎么办?你一定会问,“什么类型的车?”你不知道是否要制造汽车、雪橇或航天飞机。不存在“车辆”这样的物体。然而,“车辆”是一个有用的抽象概念,可用于对对象进行分组,指示它们之间的共同行为。这就是抽象类的用途。

An abstract class represents something that isn't specific enough to be instantiated. For instance, what if someone asked you to create a vehicle? You'd have to ask, "what kind of vehicle?" You wouldn't know whether to create a car, a sled, or a space shuttle. There's no such object as a "vehicle". Yet "vehicle" is a useful abstraction that can be used to group objects, indicating common behaviors among them. That's what abstract classes are for.

空气里的味道 2024-10-26 13:14:00

抽象类不仅仅是一个接口。它可能有数据成员。它可能具有非纯虚拟或根本非虚拟的成员函数。即使是纯虚函数也可能有一个函数体,提供默认的实现。所以这并不是说实例化抽象类在物理上是不可能的。

要点是纯虚函数是必须被派生类重写的虚函数。这意味着必须定义派生类,而强制执行此操作的方法是禁止抽象类的实例化。

抽象类不够具体,无法实例化。不一定是因为它缺少函数的定义,因为它可能没有缺少它。它不够具体,因为它代表一个抽象概念,在实例化之前必须使其更加具体。

An abstract class is more than an interface. It may have data members. It may have member functions that are not pure virtual, or non-virtual at all. Even a pure virtual function may have a body, providing a default implementation. So this is not about a physical impossibility of instantiating an abstract class.

The main point is that a pure virtual function is a virtual function that must be overridden by a derived class. That means that a derived class must be defined, and the way to force that is to forbid the instantiation of an abstract class.

An abstract class is not specific enough to be instantiated. Not necessarily because it is missing a definition of a function, because it may not be missing it. It is not specific enough because it represents an abstract concept, which must be made more specific before it can be instantiated.

醉城メ夜风 2024-10-26 13:14:00

这就是抽象类的全部要点:一些细节必须由实现者提供。

想一想:如果可以直接实例化一个类,那么将它标记为抽象类有什么意义呢?那么它就和其他班级没有什么不同了。

That's the whole point of an abstract class: that some details must be provided by the implementor.

Think about it: what would be the point of marking a class as abstract if you could instantiate it directly? Then it would be no different than any other class.

错爱 2024-10-26 13:14:00

抽象类无法实例化的原因是:如果执行纯虚函数怎么办?这将是一个严重的错误,最好在编译时捕获它而不是在运行时捕获它。

The reason an abstract class cannot be instantiated is: what do you do if you execute the pure virtual function? That would be a serious error, and it's better to catch that at compile-time than at runtime.

如果没有你 2024-10-26 13:14:00

在抽象类中,没有给出方法定义,只提供了结构。如果我们可以实例化抽象类并调用这些方法,那将是一个巨大的混乱。抽象类用于维护代码的设计模式。

In abstract class no method definition is given, only structure is provided. If we could instantiate abstract class and call those method, it will be a huge mess. Abstract class is use to maintain a design pattern of the code.

内心荒芜 2024-10-26 13:14:00

只有 Chuck Norris 可以实例化抽象类。

https://api.chucknorris.io/jokes/ye0_hnd3rgq68e_pfvsqqg

Only Chuck Norris can instantiate an abstract class.

https://api.chucknorris.io/jokes/ye0_hnd3rgq68e_pfvsqqg

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文