+ 3
How to solve this inheritance issue
Hi testA : public testA11 , public testA12 I understand that testA has two copy of display one each from testA11 and testA12 How to solve this issue or there is no solution to this ? https://code.sololearn.com/cxuqwD411qVt/?ref=app
10 Answers
+ 9
It looks like your code is implementing multiple inheritance, where a class (test and testA in this case) is derived from multiple base classes (test1, test2, testA1, and testA2 in this case). In your code, both test and testA classes have multiple base classes with methods of the same name (display). This can cause ambiguity, as the compiler will not know which display method to call when an object of these classes is created.
One way to solve this issue is to use virtual inheritance, as you have done for the testA class. Virtual inheritance ensures that only one instance of a base class is inherited by the derived class, even if multiple base classes inherit from the same base class. This can help avoid ambiguity by ensuring that there is only one display method in the derived class.
+ 9
Another way to solve the ambiguity issue is to use qualification (also known as scope resolution) to specify which display method you want to call. In your code, you have done this in the test class by using the syntax test1::display() and test2::display() to call the display methods of the test1 and test2 base classes, respectively. This allows you to call both display methods, but it still leaves the possibility of ambiguity if you want to call the display method of the derived class itself. To avoid this, you can add a display method to the derived class, as shown below:
class test : public test1, public test2
{
public:
void display()
{
test1::display();
test2::display();
}
// added display method for derived class
void own_display()
{
cout << "display of test\n";
}
};
+ 9
With this change, you can avoid using virtual inheritance and still avoid ambiguity by using qualification to call the display methods of the base classes and by calling the own_display method to call the display method of the derived class itself.
+ 4
You can override testA display method or call one of ambiguos inherited display method.
Ex:
ta.testA11::display();
+ 4
you inherit 2 virtual. it is ambigous.
python have mro
https://data-flair.training/blogs/python-multiple-inheritance/
but with c++
https://stackoverflow.com/questions/3310910/method-resolution-order-in-c
+ 3
Ketan Lalcheta Dont worry... Anyway, its clear that i (at least) have not understood you real issue.. What you mean for not supported feature? Can you explain better? In your code you try to call display method and compiler complain about it because it cannot resolve which display calls, then i assumed that you refer to that issue... Sadaam Linux have explained in very good way why happen and alternatives but maybe you have to be more explicit in details so we can understand better what you mean for issue
+ 3
Ketan Lalcheta your ambiguity is because you did not define your display method, so the program does not know which one to use.
Do the same as the previous one
class testA : public testA11 , public testA12
{
public:
void display()
{
testA11::display();
testA12::display();
}
};
0
Thanks Sadaam Linux and KrOW
But honestly speaking, i could not get the point you wanna make
What my query is about ambiguity even with virtual inheritance... isnt it or my virtual inheritance is wrong ?
I dont want scope resolution call as i am aware and is there in my first class
My question is about issue with virtual inheritance... is this not supported feature ? I dont want to call both of them unlike i did in first class. With this option as ommited, what is way or there is no way at all ?
p.s. : plz dont get me wrong...
0
Yes, it is ambiguity between 2 display
But inheritance is virtual. So, it doesnot resolve ? Virtual inheritance is useful in diamond problem only ?
0
Lines 56-63 can be deleted. Donât forget to update line 73. Since scope resolution was used in the test class display function, there is no need for any virtual inheritance.