Same function name in different classes in C++

Let’s suppose a class is derived from two base classes

In this situation, the two base classes have functions that are with the same name say fun();

Guess what would happen? If the derived class object tries to call the base fun() by;

Of course, an error will generate

But why?

Because the compiler can’t figure out fun() of which base class you would like to call


Now, the easy way to solve this problem is to use the scope resolution operator(::) to identify the desired fun() which you would like to call

That’s it!

derived d;
d.f();//an error will generate

Here base1 means first base class and base2 means second base class

To conclude, you can identify your desired fun() with scope resolution when there are many fun()available in different classes with the same name

Pretty effective right?

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.