简体   繁体   中英

Multiple inheritance C++ when bases classes share the same name for a method, is there some limit?

I've never used multiple inheritance in C++ before, but i got curious since i've seen an implementation of a braided bst, by means of a multiple inheritance of a base class List and a base class BinarySearch tree in a book.

Now i was trying to make up some stupid example in order to understand how it works.

So i came up with this:

class Base1 {
public:
    virtual void method();
};

class Base2 {
public:
    virtual void method();
};

class Derivate : public Base1, Base2 {

};

In the main i've done something like this:

Derivate d;
d.method();

The code doesn't compile, there's other code which implements both methods, but i don't post all the code because i think i make my point anyway.

To me it sounds fair that it doesn't compile because probably it isn't known which method specifically the derivate class should refer to, however it could happen that two base classes i want to extend actually shares a method name (think to both list and bst you can have a method both a method "insert" and "delete") so in general how for this "critical cases" the multiple inheritance works? in case like the one i presented is there a way to solve in a elegant way the problem?

You can write:

class Derivate : public Base1, Base2 {
public:
  using Base1::method;
};

In case of cases like this, you got to mention the class of the method that you want to call. . . Found a very detailed answer to the question here

I think it may be an example to show the magic power of the virtual method in cpp.In the main you can use a pointer to have some interesting output.

#include <iostream>
using namespace std;
class Base1 {
public:
    virtual void method(){
        cout<<"Base1 method"<<endl;
    };
};

class Base2 {
public:
    virtual void method(){
        cout<<"Base2 method"<<endl;
    };
};

class Derivate : public Base1, Base2 {
public:
    void method(){
        cout<<"Derivate method"<<endl;
    }
};
int main() {
    Derivate d;
    Base2 bs2;
    Base1 bs1;
    Base1* ptr=&bs1;
    ptr->method();
    ptr=&d;
    ptr->method();
}

In this way ,you can have

Base1 method
Derivate method

as an output.

And if you don't rewrite the method in class Derivate ,just like:

class Derivate : public Base1, Base2 {
public:
    void method(){
        //cout<<"Derivate method"<<endl;
    }
};

in the main, if you use Base1* ptr=&d , ptr->method() you will call the method in Base1;else if you use Base2* pt=&d , pt->method() you will call the method in Base2. Moreover,you can also rewrite the method maybe like:

class Derivate : public Base1, Base2 {
public:
    virtual void method(){
        Base1::method();
        Base2::method();
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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