What is the ambiguity caused by diamond inheritance

First look at this code:

#include<iostream>
using namespace std;

class Grandam
{
public:
    void introduce_self()
    {
        cout << "I am grandam." << endl;
    }
};

class Mother :public Grandam
{
public:
    void introduce_self()
    {
        cout << "I am mother." << endl;
    }
};

class Aunt :public Grandam
{
public:
    void introduce_self()
    {
        cout << "I am aunt." << endl;
    }
};

class Daughter :public Mother,public Aunt
{
public:
    void introduce_self()
    {
        cout << "I am daughter." << endl;
    }
};

int main()
{
    Grandam* ptr;
    Daughter d;
    ptr = &d;
    ptr->introduce_self();
    return 0;
}

When the program is compiled, there is a problem as shown in the figure:
Write picture description here
This is an ambiguity problem . Both Mother and Aunt inherit Grandam, while Daughter inherits Mother and Aunt. So when calling object d, the compiler does not know that the call comes from Mother. The member function introduce_self() in Aunt still comes from introduce_self() in Aunt, so errors will occur during compilation.

For a detailed analysis of the ambiguity resolution process, see the next article:
http://blog.csdn.net/lxp_mujinhuakai/article/details/69427582

Guess you like

Origin blog.csdn.net/lxp_mujinhuakai/article/details/69414277