指向基类的基本多态指针 [英] Basic polymorphic pointers to base classes

查看:71
本文介绍了指向基类的基本多态指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我使用c ++已有一段时间,但直到现在我都不需要使用多态功能,对此我非常感兴趣.

While I've been working in c++ for a while, I haven't had need to use polymorphic features until now, and I'm very intrigued by them.

如果我有一个基类ClassA并且从中派生了另一个ClassB,则我了解到我可以在ClassA中具有virtual成员函数,当在ClassB中实现时,该成员函数将在ClassB实例,即使该实例使用ClassA指针指向.如果没有此virtual关键字,我认为使用基类指针时将优先使用基类实现,但要对从子类实例化的对象进行操作,如果实际上ClassB具有自己的实现,这对我来说似乎是个问题在这种情况下实际上被忽略的功能相同.

If I have a base class ClassA and another ClassB derives from it, I understand that I can have virtual member function in ClassA that, when implemented in ClassB, will be called in a ClassB instance even if that instance is pointed at using a ClassA pointer. Without this virtual keyword, I presume the base class implementation would prevail when using a base class pointer, yet be operating on an object that was instantiated from the subclass, which seems questionable to me if in fact ClassB has its own implementation of the same function that is effectively ignored in such a case.

这是对多态行为的正确理解吗?

Is this a correct understanding of polymorphic behavior?

现在真正的问题是如何使用指向基类的指针来引用ClassB.我真的只能想到两种方式:

Now the real question is how do you refer to ClassB using a pointer to is base class. I can really only think of two ways:

  1. 在实例化时使用返回基类指针的函数来创建指针,而实际上是使用子类的构造函数为子类分配内存. (这样的创建函数有一个通用名称吗?)
  2. 使用static_cast投射对象并将其分配给指向基类的指针.
  1. Create the pointer at the time of instantiation, using a function that returns a base class pointer while actually allocating memory for the subclass instead, using the subclass's constructor. (Does such a creation function have a common name?)
  2. Casting an object using static_cast and assigning it to a pointer to the base class.

这是生成指向子类对象的基类指针的两种主要技术吗?

Are these the two main techniques for generating base class pointers to objects of a subclass?

推荐答案

最简单的方法是简单地分配它,而无需强制转换:

The easiest way is to simply assign it, no cast necessary:

ClassA *ptrA = new ClassB;

您正确地需要使用virtual关键字来启用多态行为.这是一种思考方式. C ++对对象的静态类型进行操作.调用ptrA->foo()时,指针的类型为ClassA*.如果未声明该功能virtual,则它将盲目调用该功能的ClassA版本.别无选择.但是,如果foo()virtual,则它知道停下来并问:等等,我实际上是哪种类型?"在这种情况下,答案是ClassB,因此它将调用ClassB的版本.

You're correct that you need the virtual keyword to enable polymorphic behavior. Here's one way to think about it. C++ operates on the static type of an object. When you call ptrA->foo(), the type of the pointer is ClassA*. If that function is not declared virtual, then it will blindly call ClassA's version of the function. There's no other choice. But if foo() is virtual, then it knows to stop and ask, "Wait, what type am I really?" And the answer in that case is ClassB, so it will call ClassB's version.

还请注意,您不需要指针即可实现此目的.您会发现实际使用多态的另一种常见方式是通过函数调用:

Also note that you don't need pointers to achieve this. Another common way you'll see polymorphism in action is via a function call:

void bar(ClassA &aObj)
{
    aObj.foo();
}

// ...
ClassB bObj;
bar(bObj);

这篇关于指向基类的基本多态指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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