Code -
#include<iostream>
using namespace std;
class P {
public:
void print() { cout <<" Inside P"; }
};
class Q : public P {
public:
void print() { cout <<" Inside Q"; }
};
class Q2: public P {
public:
void print2() { cout <<" Inside Q2"; }
};
class R: public Q2, public Q { };
int main(void)
{
R r;
r.print(); // error: request for member ‘print’ is ambiguous
return 0;
}
Expected behavior: No ambiguous call on 3rd line in main.
Actual behavior: Ambiguous call on 3rd line in main
Rationale: I expected class Q to hide the print() function of class P. I would expect an ambiguous call error when Q2 has the same function name as R or Q (or even P?). But I deliberately changed the function name in Q2 to 'print2' to avoid this error. This error goes away when I remove 'Q2' as parent class of R. Is this happening because 'Q2' inherits 'print' from 'P'?
Note: I know similar questions have been asked on data hiding for regular inheritance and ambiguous calls in case of inheritance from multiple classes, but this case is sort of a mixture of two, and I did not find any specific answers.