虚拟继承/多态性

Virtual inheritance / Polymorphism

本文关键字:多态性 继承 虚拟      更新时间:2023-10-16

所以我正在研究一些奇怪的多食症和多义性。有一个案例我无法理解。我不明白为什么下面代码的最后一行会这样。

#include <stdio.h>
#include <iostream>
class Article
{
public :
    virtual bool compare(Article *a) { std::cout << "ACA" << std::endl;}; // ACA
};
 class Clothe : public Article
 {
    public :
        bool compare (Clothe *c) { std::cout << "CCC" << std::endl;}; // CCC
        bool compare (Article *a) { std::cout << "CCA" << std::endl;}; // CCA
};

int main()
{
Article *pta1 = new Article;
Article *pta2 = new Clothe;
Clothe *ptv1 = new Clothe;
pta1->compare(pta2); // print ACA
pta2->compare(pta1); // print CCA
pta2->compare(pta2); // print CCA because compiler needs to explore pta2 to determine which compare to use.
                     // pta2 is instanced as a Clothe, so one of Clothe's compare will be called. 
                     // And judging by its declaration pta2 is an Article (even thought it's instanced as a Clothe),
                     // so it will Call Clothe::Compare(Article*). I'm OK with this 
pta2->compare(ptv1); // print CCA ??? WHY , pta2 is explored, thus it's type is determined to be of type Clothe.
                     // And ptv1 is declared as a Clothe, so it should call Clothe::compare(Clothe* c)
                     // should print CCC, shouldn't it ?
}

我迷路了。我不明白 g++ 如何以这种方式解决歧义。任何帮助/解释都会很好。感谢您的阅读,更感谢那些回答的人。

PS :我知道比较应该返回布尔值,但这在这里并不重要。

编辑:在我即时翻译变量名称的过程中,错别字滑了,名称最初是法语的,对此感到抱歉。现已修复

此行

pta2->compare(ptv1);

打印CCA,因为基类中只有一个虚函数bool compare(Article *a)

你对pta2的假设是错误的。

探索 pta2,因此将其类型确定为 Clothe 类型。

事实并非如此。pta2的类型是Article *的,仅此而已。它只指向一个Clothe对象,因此可以通过它调用Clothe函数,如果(并且仅当)它们在派生类中存在并且是虚拟的,并且是基类并被覆盖(使用相同的签名!

简单来说:

如果调用是通过基类指针执行的,则编译器只能在派生类中"查找"重载,这些重载在基类中存在且是虚拟的。

这里发生了什么:

  1. pta2 是指向 Article 的指针。因此,在调用compare时,会考虑Articlecompare函数的不同可能性。

  2. 唯一的匹配是 bool Article::compare (Article *a) .

  3. 现在,编译器发现compare Article是虚拟的,并使用一些内部机制通过对象的"vtable"进行虚拟调用。

  4. Clothe覆盖bool compare (Article *a)时,被覆盖的函数被调用。

基类对bool compare (Clothe *c)一无所知,因为它对Clothe一无所知,因此输出不CCV