Friday, 15 June 2012

c++ - Why is multiply inheriting a method with different signatures ambiguous? -



c++ - Why is multiply inheriting a method with different signatures ambiguous? -

this question has reply here:

why multiple-inherited functions same name different signatures not treated overloaded functions? 2 answers

if inherit function same name different signatures different base of operations classes, effort phone call function generates error claiming phone call ambiguous. same functions in single base of operations class not generate error. why this?

first case, http://ideone.com/calh4q

#include <iostream> using namespace std; struct base1 { void foo(double param) { cout << "base1::foo(double)" << endl; } }; struct base2 { void foo(int param) { cout << "base2::foo(int)" << endl; } }; struct derived : public base1, public base2 { }; int main(int argc, char **argv) { derived d; d.foo(1.2); homecoming 1; } prog.cpp: in function ‘int main(int, char**)’: prog.cpp:27:7: error: request fellow member ‘foo’ ambiguous prog.cpp:14:10: error: candidates are: void base2::foo(int) prog.cpp:6:10: error: void base1::foo(double)

second case, no errors, http://ideone.com/mq3j7a

#include <iostream> using namespace std; struct base of operations { void foo(double param) { cout << "base::foo(double)" << endl; } void foo(int param) { cout << "base::foo(int)" << endl; } }; struct derived : public base of operations { }; int main(int argc, char **argv) { derived d; d.foo(1.2); homecoming 1; }

overloading occurs between names defined in same scope. when multiple bases define same name, definitions in different scopes, don't overload. if add together 2 using declarations derived can pull 2 names derived , overload.

c++ multiple-inheritance ambiguous

No comments:

Post a Comment