c++ - Cast derived virtual override to base pure virtual member -
i understand why cannot cast derived class member function pointer base class member function pointer explained here.
but, given snippet:
struct base { virtual void foo() = 0; }; struct derived : base { void foo() override {}; }; struct invoker { typedef void(base::*target)(); invoker(base* b, target t) { (b->*t)(); } }; template<typename b, typename d> void (b::*cast(void (d::*method)()))() { return static_cast<void(b::*)()>(method); } derived d; invoker bad(&d, &derived::foo); //c2664 invoker good(&d, cast<base>(&derived::foo)); i wanted ask possible decorate base function signature compiler understands pure virtual method , and implemented somewhere across hierarchy (otherwise not construct object of type b)? understand why can't normal functions, imho in case of pure virtual function compiler has guarantee implemented (in case not done error class b not cast).
there's no need manipulate type of &derived::foo. 1 can use &base::foo instead.
pointers member functions respect virtuality. call
base* pbase = new derived; auto pfoo = &base::foo; (pbase->*pfoo)(); will call derived::foo, simple call pbase->foo() would.
Comments
Post a Comment