Inheritance in C ++, "... is an ambiguous base ..."

As written in "C ++ 3.Edition Programming Language - Bjarne Stroustrup". We can use a scope solution to prevent ambiguity errors. The main program, which is given below, when I use a 3-layer region in the mix class, an error occurs. But when I use 2 layers, there is no problem. What's wrong? or is it something like a design problem? Error:

deneme.cpp: In constructor ‘mix::mix(std::__cxx11::string, int)’:
deneme.cpp:45:22: error: ‘plane’ is an ambiguous base of ‘mix’
    pervaneli::plane::engine=b;

I do not want to create a diamond model. I am pleased with the two base (planar) classes. That I just want to find out why it gives an error when I use a 3-layer area. Thank.

Note: g ++ version (Ubuntu 5.4.0-6ubuntu1 ~ 16.04.4) 5.4.0 20160609

#include<iostream>
#include<string>
using namespace std;

class plane{
    protected:
    int speed;
    string name;
    public:
        int engine;
        void its_name(){
            cout<<name<<endl;
        }
        plane(int a=10000){
            engine=a;
        }

};

class pervaneli:public plane{
    public:
        pervaneli(string a="-"){
            name=a;
        }
        void belirle(int x){
            speed=x;
        }
};

class jet:public plane{
    public:
        jet(string a="-"){
            name=a;
        }
        void belirle(int x){
            speed=x;
        }
};

class mix: public pervaneli,public jet{
    public:
        mix(string a,int b){
            jet::name=a;
            pervaneli::name=a;
            pervaneli::plane::engine=b; //ambigous base error
            //pervaneli::engine=b; /*works fine*/
        } 
        void belirle(int x){
            jet::speed=x;
        }
};


int main()
    {
    mix a400m("Alp",9999);
    a400m.belirle(500);
    a400m.pervaneli::its_name();
    return 0;
    }
+4
source share
2

qualified_lookup

pervaneli::plane - plane ().

pervaneli::plane::engine plane::engine, , plane.

, .

+6

is a.
: ++: 32: , "is-a."

, pervaneli plane - . pervaneli - engine - is a plane.

plane, , , .

+1

Source: https://habr.com/ru/post/1676284/


All Articles