简体   繁体   English

多态与常规继承?

[英]Polymorphism vs regular inheritance?

I use polymorphism so frequently, but it suddenly dawned upon me. 我如此频繁地使用多态性,但是突然间我意识到了。 I have the case where my code is: 我的情况是我的代码是:

class A{

class B : public A{

class C : public A{

and I use class A as a polymorphic parameter type for passing in B or C sub types: 我将A类用作传递BC子类型的多态参数类型:

//Accepts B and C objects
void aMethod(A* a){

However, I gave A a virtual destructor- this is the only reason A (and B and C) contain a vtable pointer. 但是,我给了A一个虚拟析构函数-这是A(以及B和C)包含一个vtable指针的唯一原因。

So my question is, if I hadn't declared A with a virtual destructor, then A wouldn't be polymorphic- I wouldn't be able to pass objects of type B or C in aMethod() ?? 所以我的问题是,如果我没有用虚拟析构函数声明A,那么A不会是多态的-我将无法在aMethod()传递B或C类型的对象?

So is non-polymorphic inheritance just about sharing code, whereas polymorphism (the base class must have a vtable) allows passing sub types as arguments of the base class type? 那么非多态继承是否只是共享代码,而多态(基类必须具有vtable)允许将子类型作为基类类型的参数传递呢?

In this code you provide, 在您提供的这段代码中,

void aMethod(A a){

the a formal argument is not polymorphic. a正式的说法是不是多态。 It's passed by value. 它通过价值传递。 When you pass in a B or C object as actual argument, you're just slicing it down to an A , that is, you're copying the A base class sub-object only. 当您将BC对象作为实际参数传递时,您只是切成 A ,也就是说,您仅复制A基类的子对象。


Regarding 关于

So is non-polymorphic inheritance just about sharing code, whereas polymorphism (the base class must have a vtable) allows passing sub types as arguments of the base class type? 非多态继承就是共享代码吗,而多态(基类必须具有vtable)允许将子类型作为基类类型的参数传递?

it combines two questions that have to be treated separately. 它结合了两个必须分别处理的问题。

Inheriting from a non-polymorphic class is about sharing of code, yes, but it also introduces an is-a relationship, which can be important for eg passing by reference. 从非多态类继承是关于代码共享的,是的,但是它还引入了is-a关系,这对于例如通过引用传递可能很重要。

Inheriting from a polymorphic class (one with one or more virtual member functions) allows you to override base class functionality, or to implement it where the base class leaves that as a derived class responsibility – typically by having pure virtual member functions. 从多态类(一个具有一个或多个虚拟成员函数的类)继承可让您重写基类功能,或在基类将其作为派生类职责的位置实现(通常通过具有纯虚拟成员函数)。 This means that member functions in the base class can call your derived class' member functions. 这意味着基类中的成员函数可以调用派生类的成员函数。 Also, that calling member functions via a base class pointer or reference, can call your derived class's implementations. 同样,通过基类指针或引用调用成员函数可以调用派生类的实现。

Non-polymorphic types also contain a subobject for each base class, and upcasts are still implicit. 非多态类型还为每个基类包含一个子对象,并且向上转换仍然是隐式的。

You can therefore pass objects of derived classes just fine even when there is no polymorphism. 因此,即使没有多态性,也可以很好地传递派生类的对象。 The receiver will act on the base subobject, just as happens for data members and non-virtual functions of polymorphic types. 接收器将对基础子对象起作用,就像多态类型的数据成员和非虚拟函数一样。

A class with its destructor the only virtual method, like your class A , is an oddity. 带有析构函数的唯一virtual方法的class A (例如您的class A )很奇怪。 AFAIK, such a class has no necessity in well designed code. AFAIK,这种类不需要精心设计的代码。

From your question, it is not clear whether the virtual destructor is needed (ie if you ever call the destructor of classes B or C through a reference or pointer to class A ), but if it is, your code smells. 从您的问题出发,尚不清楚是否需要virtual析构函数(即是否曾经通过对类A的引用或指针来调用类BC的析构函数),但如果是,则代码会发出臭味。 If it is not, then simply make the destructor non-virtual. 如果不是,则只需将析构函数设为非虚拟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM