A
Alex Shen
Hi all, pls consider the following code,
#include <iostream>
struct base
{
virtual void f()
{
std::cout << "base::f\n";
}
};
struct derived : base
{
virtual void f()
{
std::cout << "derived::f\n";
}
};
int main()
{
typedef void (base::* base_ptr_type)();
typedef void (derived::* derived_ptr_type)();
derived_ptr_type pd(&derived::f);
base_ptr_type pb(reinterpret_cast<base_ptr_type>(pd));
derived d;
(static_cast<base*>(&d)->*pb)(); // output "derived::f"
}
IIRC, MFC uses this technique in it's message mapping implmentation.
It's not portable, right? Because of the use of reinterpret_cast whose
result is undefined as the standard says.
Alex
#include <iostream>
struct base
{
virtual void f()
{
std::cout << "base::f\n";
}
};
struct derived : base
{
virtual void f()
{
std::cout << "derived::f\n";
}
};
int main()
{
typedef void (base::* base_ptr_type)();
typedef void (derived::* derived_ptr_type)();
derived_ptr_type pd(&derived::f);
base_ptr_type pb(reinterpret_cast<base_ptr_type>(pd));
derived d;
(static_cast<base*>(&d)->*pb)(); // output "derived::f"
}
IIRC, MFC uses this technique in it's message mapping implmentation.
It's not portable, right? Because of the use of reinterpret_cast whose
result is undefined as the standard says.
Alex