Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do multiple-inherited functions with same name but different signatures not get treated as overloaded functions?

The following snippet produces an "ambigious call to foo" error during compilation, and I'd like to know if there is any way around this problem without fully qualifying the call to foo:

#include <iostream>  struct Base1{     void foo(int){     } };  struct Base2{     void foo(float){     } };  struct Derived : public Base1, public Base2{ };  int main(){     Derived d;     d.foo(5);      std::cin.get();     return 0; } 

So, question is as the title says. Ideas? I mean, the following works flawlessly:

#include <iostream>  struct Base{     void foo(int){     } };  struct Derived : public Base{     void foo(float){     } };  int main(){     Derived d;     d.foo(5);      std::cin.get();     return 0; } 
like image 608
Xeo Avatar asked Mar 20 '11 13:03

Xeo


People also ask

Is possible when a derived class function has the same name and signature as its base class?

In C++, function overloading is possible i.e., two or more functions from the same class can have the same name but different parameters. However, if a derived class redefines the base class member method then all the base class methods with the same name become hidden in the derived class.

Why should we avoid multiple inheritance?

Allowing multiple inheritance makes the rules about function overloads and virtual dispatch decidedly more tricky, as well as the language implementation around object layouts. These impact language designers/implementors quite a bit and raise the already high bar to get a language done, stable, and adopted.

How does multiple inheritance work?

Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or class may only inherit from one particular object or class.

How do you show multiple inheritance?

Multiple Inheritance in C++ Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The constructors of inherited classes are called in the same order in which they are inherited. For example, in the following program, B's constructor is called before A's constructor.


1 Answers

Member lookup rules are defined in Section 10.2/2

The following steps define the result of name lookup in a class scope, C. First, every declaration for the name in the class and in each of its base class sub-objects is considered. A member name f in one sub-object B hides a member name f in a sub-object A if A is a base class sub-object of B. Any declarations that are so hidden are eliminated from consideration. Each of these declarations that was introduced by a using-declaration is considered to be from each sub-object of C that is of the type containing the declara-tion designated by the using-declaration. If the resulting set of declarations are not all from sub-objects of the same type, or the set has a nonstatic member and includes members from distinct sub-objects, there is an ambiguity and the program is ill-formed. Otherwise that set is the result of the lookup.

class A { public:   int f(int);  }; class B { public:    int f();  }; class C : public A, public B {}; int main() {      C c;      c.f(); // ambiguous } 

So you can use the using declarations A::f and B::f to resolve that ambiguity

class C : public A, public B {      using A::f;      using B::f;  };  int main() {      C c;      c.f(); // fine } 

The second code works flawlessly because void foo(float) is inside C's scope. Actually d.foo(5); calls void foo(float) and not the int version.

like image 68
Prasoon Saurav Avatar answered Sep 18 '22 11:09

Prasoon Saurav