D
Dave
Hello all,
Quoting from page 24 of "The Boost Graph Library; User Guide and Reference
Manual":
"It turns out that by the contravariance subtyping rule, the parameter type
in the derived classes member function must be either the same type or a
base class of the type as the parameter in the base class."
Now please consider this code:
#include <iostream>
struct base_1 {};
struct derived_1: base_1 {};
struct base_2
{
virtual void foo(derived_1 *p) {std::cout << "base_2::foo()\n";}
};
struct derived_2: base_2
{
virtual void foo(base_1 *p) {std::cout << "derived_2::foo()\n";}
};
int main()
{
base_2 *ptr = new derived_2;
ptr->foo(new derived_1);
}
This outputs base_2::foo(). Why?
The quoted passage implies derived_2::foo() should override base_2::foo().
Clearly, it does not. Furthermore, I cannot find anything in the Standard
that indicates it should. So, I am clearly misinterpreting the quoted
passage. Can anybody explain to me what was meant in that passage?
Thank you,
Dave
Quoting from page 24 of "The Boost Graph Library; User Guide and Reference
Manual":
"It turns out that by the contravariance subtyping rule, the parameter type
in the derived classes member function must be either the same type or a
base class of the type as the parameter in the base class."
Now please consider this code:
#include <iostream>
struct base_1 {};
struct derived_1: base_1 {};
struct base_2
{
virtual void foo(derived_1 *p) {std::cout << "base_2::foo()\n";}
};
struct derived_2: base_2
{
virtual void foo(base_1 *p) {std::cout << "derived_2::foo()\n";}
};
int main()
{
base_2 *ptr = new derived_2;
ptr->foo(new derived_1);
}
This outputs base_2::foo(). Why?
The quoted passage implies derived_2::foo() should override base_2::foo().
Clearly, it does not. Furthermore, I cannot find anything in the Standard
that indicates it should. So, I am clearly misinterpreting the quoted
passage. Can anybody explain to me what was meant in that passage?
Thank you,
Dave