是什么让某些东西成为 C++ 中的抽象类?
可能的重复:
具体类之间有什么区别和一个抽象类?
我正在 Visual C++ 2008 中编写一些代码,并在我正在阅读的一本书中做练习,当我将光标悬停在其中一个类上时,它告诉我这是一个抽象类。现在我知道它是一个抽象类,因为这就是本练习的目的,但我很好奇是什么让 Intelisense 知道它是一个抽象类。
我做了一点功课,发现可能是因为我在这个类中有两个虚函数,其中一个是纯虚函数。
纯虚拟是一个致命的赠品,还是有其他东西可以告诉您您正在处理或查看抽象类?
Possible Duplicate:
What is the difference between a concrete class and an abstract class?
I was coding something in Visual C++ 2008 doing an exercise in a book I am reading when I held my cursor over one of the classes and it told me it was an abstract class. Now I know it is an abstract class as that is what this exercise is about but I was curious as to what lets the Intelisense thing know that it is an abstract class.
I did a little homework and found that it may have been the fact that I have two virtual functions in this class and one of them is a pure virtual.
Is the pure virtual a dead giveaway or are there other things that would tell you you are dealing with or looking at an abstract class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C++ 中, ?是的。由于它没有
abstract
关键字或等效关键字,因此使类抽象的常见习惯用法是在其中声明一个纯虚函数(这会阻止实例化)。*出于同样的原因,在 C++ 中没有接口和抽象类之间有很大区别 - C++接口只是一个仅包含纯虚函数的类。
*更新:防止类实例化的另一种方法是将其构造函数声明为
protected
或private
。后者意味着它也不能被子类化,但前者并不阻止子类化,因此理论上受保护的构造函数也可以是抽象类的标志一种强制子类化的方法。然而,我在实践中从未见过这种情况。我相信这是因为抽象类被设计为可扩展的,这在实践中几乎总是意味着它具有虚函数。我们之所以希望它是抽象的,是因为它的某些部分的实现尚不清楚,因此它需要在其子类中定义。这正是纯虚函数的用途。In C++, yes. Since it has no
abstract
keyword or equivalent, the common idiom to make a class abstract is to declare a pure virtual function in it (which prevents instantiation).*For this same reason, in C++ there is not much difference between an interface and an abstract class - a C++ interface is simply a class containing only pure virtual functions.
*Update: Another way to prevent instantiation of a class is to declare its constructor(s)
protected
orprivate
. The latter means it can't be subclassed either, but the former doesn't prevent subclassing, so in theory a protected constructor could also bea sign of an abstract classa way to force subclassing. However, I have never seen this in practice. I believe this is because an abstract class is designed to be extended, which in practice almost always means it has virtual functions. And the reason why we want it to be abstract is because some part of its implementation is not yet known, hence it is to be defined in its subclass(es). Which is exactly what a pure virtual function is meant for.使类抽象的唯一因素是它是否具有一个或多个未实现的纯虚函数(可能是派生函数)。
请注意,A 还应该有一个(可能不是纯的)虚拟析构函数。
也可以实现纯虚函数,但这种情况非常罕见:
函数必须在类外部定义:
但类仍然是抽象的。
The only thing that makes a class abstract is if it has one or more unimplemented pure virtual functions, possibly derived ones.
Note that A should also have a (probably not pure) virtual destructor.
It is also possible to implement pure virtual functions, but this is pretty rare:
The function has to be defined outside the class:
but the class remains abstract.
我不会说我是 C++ 方面的专家,但我会说你已经掌握了它。纯虚方法的存在意味着该类无法实例化,因此是抽象的。如果您的所有方法都是纯虚拟的(即没有实现),那么它将是一个接口,在这种情况下您只需要头文件。
I wouldn't say I'm an expert in C++ but I would say you've nailed it. The presence of a pure virtual method means that the class cannot be instantiated and is therefore abstract. If all of your methods are pure virtual (i.e. there is no implementation) it would be an interface and in this case you only need the header file.