为什么这段代码"not ambigious!" - 虚函数

Why is this piece of code "not ambigious!" - virtual functions

本文关键字:ambigious not 函数 段代码 代码 为什么      更新时间:2023-10-16

为什么下面的代码不含糊,以及它是如何正常工作的?

#include <QCoreApplication>
#include <iostream>
using namespace std;
class Shape{
public:
    virtual void drawShape(){
        cout << "this is base class and virtual functionn";
    }
};
class Line : public Shape{
public:
    virtual void drawShape(){
        cout << "I am a linen";
    }
};
class Circle : public Shape{
public:
    virtual void drawShape(){
        cout <<" I am circlen";
    }
};
class Child : public Line, public Circle{
public:
    virtual void drawShape(){
        cout << "I am child :)n";
    }
};
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    //Shape *s;
    //Line l;
    Child ch;
    //s = &l;
    //s = &ch;
    ch.drawShape(); // this is ambiguous right? but it executes properly!
    //s->drawShape();
    return a.exec();
}

这并不含糊,因为Child定义了它自己对drawShape的覆盖,而ch.drawShape将调用该函数。如果Child没有提供对drawShape的覆盖,那么该调用将是不明确的。