Is it safe to “upcast” a method pointer and use it with base class pointer?
Let's say I have a pointer type that can hold the address of a base class method. Can I assign the address of a subclass method to it and expect it to work correctly? In my case I'm using it with a base class pointer and the dynamic type of the object is the derived class. struct B { typedef void (B::*MethodPtr)(); }; struct D: public B { void foo() { cout<<"foo"<<endl; } }; int main(int argc, char* argv[]) { D d; B* pb = &d; //is the following ok, or undefined behavior? B::MethodPtr mp = static_cast<B::MethodPtr>(&D::foo); (pb->*mp)(); } The standard says this when talking about static_cast: