Multiple Inheritance
suggest changeAside from single inheritance:
class A {};
class B : public A {};
You can also have multiple inheritance:
class A {};
class B {};
class C : public A, public B {};
C now inherits from both A and B.
This can lead to ambiguity when multiple base classes expose the same name.
Ambiguity in multiple inheritance
If two accessible base classes provide functions with the same name and the derived class does not resolve the ambiguity, an unqualified call through the derived object is ill-formed:
class Base1 {
public:
void function() {}
};
class Base2 {
public:
void function() {}
};
class Derived : public Base1, public Base2 {};
int main() {
Derived object;
object.function(); // Error: Base1::function or Base2::function?
}
Qualify the call to select a base-class member explicitly:
int main() {
Derived object;
object.Base1::function();
object.Base2::function();
}
Alternatively, Derived can provide its own function() or use a using declaration to choose one base-class overload set.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents