抽象方法和纯虚函数是相同的东西吗? [英] Are abstract methods and pure virtual functions the same thing?

查看:208
本文介绍了抽象方法和纯虚函数是相同的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,抽象方法和纯虚函数都不提供任何功能......所以我们可以说它们都是同样的东西?



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

解决方案

,它们是相同的。在C ++中,抽象方法只是描述纯虚函数特征的另一种方式。这两者都只是一种没有实现的方法,只要在实际实例化类之前需要在子类中实现。

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

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

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

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 ?

解决方案

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.

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.

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

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

这篇关于抽象方法和纯虚函数是相同的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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