简体   繁体   English

抽象类继承具有相同功能名称的另一个抽象类

[英]Abstract class inheriting another abstract class with the same function name

 class subscriber
 {
 public:
      virtual void update() = 0;
 }

 class entity : public subsriber
 {
 public:
      virtual void update() = 0;
 }

 class myObject : public entity
 {
 public:
      virtual void update()
       {
        do_things();
       }
 }

 subscriber * ptr = new myObject; //will use shared_ptr, but here i want simplicity

  ptr->update(); 

The question is, will the proper update function (the one implemented in myObject) be called? 问题是,是否将调用适当的更新功能(在myObject中实现的更新功能)? And is it valid to have 2 pure virtual functions with the same name in one "family"? 在一个“家族”中拥有两个具有相同名称的纯虚拟函数是否有效?

will the proper update function (the one implemented in myObject) be called? 将调用适当的更新功能(在myObject中实现的更新功能)吗?

Yes, it will be called. 是的,它将被调用。

is it valid to have 2 pure virtual functions with the same name in one "family"? 在一个“家族”中拥有两个具有相同名称的纯虚拟函数是否有效?

The second declaration (ie inside the entity class) does not introduce a second pure virtual function into the family: the signatures are identical, so update() is a single virtual function. 第二个声明(即在entity类内部)没有向家族引入第二个纯虚函数:签名是相同的,因此update()是单个虚函数。 Moreover, declaring it for the second time is not necessary: entity would remain abstract, and would have access to the update() method even if you removed the second declaration. 而且,没有必要第二次声明它: entity将保持抽象状态,并且即使您删除了第二个声明也可以访问update()方法。

A virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a function with the same signature. 虚函数或虚方法是一种函数或方法,其行为可以在继承类内被具有相同签名的函数覆盖。

So the answer is yes . 所以答案是肯定的

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

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