Monday, May 9, 2022

ambiguity resolution in multiple inheritance

problem arises sometimes while dealing with multiple inheritance 

for e.g

sometimes when a derived class which is inherited from  multiple base classes which have a method

with same name and return type but derived has no such method of its own 

in this case when we try to call that function/method using derived class object it causes ambiguity 

because it confuses the compiler between the method definitions and choosing which one to call 

in that case we use a scope resoluion operator along with class name and method to be called 

its shown in the following code



 #include <iostream>

using namespace std;
class A
{
public:
    void show()
    {
        cout << "hi\n";
    }
};
class b
{
public:
    void show()
    {
        cout << "hi from b\n";
    }
};
class c : public A, public b
{

    public:
    void show() { A::show(); }
};
int main()
{
   
    c ce;
    ce.show();
    return 0;
}

No comments:

HackCache ’26: A 48-Hour Online Hackathon Open to Every Student

  Hackathons are often associated with computer science students, experienced developers, and teams that already know exactly what they are...

Popular Posts