J
Juha Nieminen
It occurred to me while developing an application: What happens
if two (completely independent) base classes have the exact same
virtual function, and then a derived class is derived from both,
and this derived class implements that function? Will it work?
Will it cause an error along the lines of "ambiguous function
definition" or whatever?
Well, I tried it, and it actually seemed to compile and work
just as expected. Apparently this *is* a standardized behavior?
Does this have some kind of name in the standard?
#include <iostream>
class Base1
{
public:
virtual void method(int i) = 0;
};
class Base2
{
public:
virtual void method(int i) = 0;
};
class Derived: public Base1, public Base2
{
public:
virtual void method(int i)
{
std::cout << i << std::endl;
}
};
int main()
{
Derived d;
Base1& b1 = d;
Base2& b2 = d;
d.method(1);
b1.method(2);
b2.method(3);
}
if two (completely independent) base classes have the exact same
virtual function, and then a derived class is derived from both,
and this derived class implements that function? Will it work?
Will it cause an error along the lines of "ambiguous function
definition" or whatever?
Well, I tried it, and it actually seemed to compile and work
just as expected. Apparently this *is* a standardized behavior?
Does this have some kind of name in the standard?
#include <iostream>
class Base1
{
public:
virtual void method(int i) = 0;
};
class Base2
{
public:
virtual void method(int i) = 0;
};
class Derived: public Base1, public Base2
{
public:
virtual void method(int i)
{
std::cout << i << std::endl;
}
};
int main()
{
Derived d;
Base1& b1 = d;
Base2& b2 = d;
d.method(1);
b1.method(2);
b2.method(3);
}