D
Dave Rahardja
Consider the following example:
---
#include <iostream>
class Base
{
public:
virtual void fn() { std::cout << "Base::fn()\n"; }
};
typedef void (Base::*Base_fn_ptr)();
class Derived: public Base
{
public:
virtual void fn() { std::cout << "Derived::fn()\n"; }
static Base_fn_ptr Get_Base_Fn_Ptr() { return &Base::fn; }
};
int main()
{
Derived obj;
Base_fn_ptr ptr = Derived::Get_Base_Fn_Ptr();
(obj.*ptr)();
return 0;
}
---
Running the example causes "Derived::fn()" to be output. Is there a way
to craft Get_Base_Fn_Ptr() so that it returns the address of Base::fn()
instead of a virtual function-calling stub?
In other words, I want to defeat the polymorphic behavior of obj.
- Dave Rahardja
---
#include <iostream>
class Base
{
public:
virtual void fn() { std::cout << "Base::fn()\n"; }
};
typedef void (Base::*Base_fn_ptr)();
class Derived: public Base
{
public:
virtual void fn() { std::cout << "Derived::fn()\n"; }
static Base_fn_ptr Get_Base_Fn_Ptr() { return &Base::fn; }
};
int main()
{
Derived obj;
Base_fn_ptr ptr = Derived::Get_Base_Fn_Ptr();
(obj.*ptr)();
return 0;
}
---
Running the example causes "Derived::fn()" to be output. Is there a way
to craft Get_Base_Fn_Ptr() so that it returns the address of Base::fn()
instead of a virtual function-calling stub?
In other words, I want to defeat the polymorphic behavior of obj.
- Dave Rahardja