C++ 抽象方法和纯虚函数是一回事吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6574852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:22:47  来源:igfitidea点击:

Are abstract methods and pure virtual functions the same thing?

c++functionvirtual

提问by Ahmad

As far as I know, both abstract methods and pure virtual functions do NOT provide any functionality ... So can we say they're both the same thing ?

据我所知,抽象方法和纯虚函数都不提供任何功能......所以我们可以说它们都是一回事吗?

Also, suppose a class (not necessarily declared as abstract) contains a number of implemented methods (not abstract or virtual), but contains a pure virtual function. Is this class then abstract ?

此外,假设一个类(不一定声明为抽象)包含许多已实现的方法(不是抽象或虚拟),但包含一个纯虚函数。这个类是抽象的吗?

回答by Gordon Gustafson

Yes, they are the same thing. In C++, an abstract method is just another way of describing the characteristics of a pure virtual function. Both just mean a method with no implementation provided that needs to be implemented in a sub-class before the class can actually be instantiated.

是的,它们是同一回事。在 C++ 中,抽象方法只是描述纯虚函数特性的另一种方式。两者都只是意味着一个没有提供实现的方法,需要在子类中实现,然后才能实际实例化该类。

The situation with pure virtual functions and abstract classes in C++ is similar as they essentially mean exactly the same thing. Any abstract class must have at least 1 pure virtual function or else it could be instantiated and wouldn't be abstract. Likewise, any class with at least 1 pure virtual function must be abstract because it needs to be extended so that method can actually be implemented.

C++ 中纯虚函数和抽象类的情况类似,因为它们本质上是完全一样的。任何抽象类必须至少有 1 个纯虚函数,否则它可以被实例化并且不会是抽象的。同样,任何具有至少 1 个纯虚函数的类都必须是抽象的,因为它需要进行扩展,以便可以实际实现方法。

Therefore, a class is abstract if and only ifit contains at least 1 pure virtual function/abstract method.

因此,一个类是抽象的,当且仅当它包含至少 1 个纯虚函数/抽象方法。

Later on, languages like Java and C# made things like this more explicit, allowing a special keyword to define a class abstract rather than the presence of a pure-virtual function. C++ lets you do the same things as these languages, but they're just a little more explicit about it. :D

后来,Java 和 C# 之类的语言使这样的事情更加明确,允许使用特殊关键字来定义类抽象而不是纯虚函数的存在。C++ 可以让您做与这些语言相同的事情,但它们只是更明确一点。:D

回答by Jord?o

You don't explicitly declare classes or methods as abstract in C++. The presence of pure virtual methods is what makes them abstract.

您没有在 C++ 中将类或方法显式声明为抽象类。纯虚方法的存在使它们变得抽象。

回答by David Titarenco

Yes, abstract methods are the exact same thing as pure virtual functions; the terms are often used interchangeably. IMO, "Pure virtual function" is the C++ technically correct term which specifically denotes the fact that the function is set to 0:

是的,抽象方法与纯虚函数完全相同;这些术语经常互换使用。IMO,“纯虚函数”是 C++ 技术上正确的术语,它专门表示函数设置为0

class myClass {
public:
  virtual void vfunc() = 0; // pure specifier
};

An abstract class is defined by:

抽象类定义为

a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function.

专门用作基类的类。一个抽象类至少包含一个纯虚函数。

So basically, an abstract class is an abstract class because it's designed to be a base class (some base classes by definition need to have implementable methods which will need to be pure virtual). These classes become abstract classes simply by how they are used and extended from. Unlike languages like Java, there is no abstractor interfacekeyword modifier so this is why we need a "verbal contract" to talk about abstract classes in C++.

所以基本上,抽象类是抽象类,因为它被设计为基类(根据定义,某些基类需要具有可实现的方法,这些方法需要是纯虚拟的)。这些类只是根据它们的使用和扩展方式而成为抽象类。与 Java 等语言不同,没有abstractorinterface关键字修饰符,这就是为什么我们需要“口头约定”来谈论 C++ 中的抽象类。

回答by Lightness Races in Orbit

In C++, a pure virtual member function leads to the enclosing type being an "abstract type".

在 C++ 中,纯虚成员函数导致封闭类型成为“抽象类型”。

Functions themselves cannot be abstract, though the term is frequently misused in this manner.

函数本身不能是抽象的,尽管该术语经常以这种方式被误用。

回答by Akhil

I would say yes, abstract methods and pure virtual functions are conceptually the same thing.

我会说是的,抽象方法和纯虚函数在概念上是一回事。

Also, suppose a class (not necessarily declared as abstract) contains a number of implemented methods (not abstract or virtual), but contains a pure virtual function. Is this class then abstract ?

此外,假设一个类(不一定声明为抽象)包含许多已实现的方法(不是抽象或虚拟),但包含一个纯虚函数。这个类是抽象的吗?

A class with at least 1 pure virtual function is called an abstract class.

具有至少 1 个纯虚函数的类称为抽象类。