Function pointers to member functions of a parent class

两盒软妹~` 提交于 2019-12-04 13:11:39

A void (Parent::*)() takes an object of type Parent. The function Child::afunc requires an object of type Child. Converting the pointer to void (Parent::*)() would therefore allow for an invalid call.

Member function pointers can't really be used to implement polymorphism. The traditional way to implement polymorphism without using virtual is to use regular function pointers:

struct MyBaseClass
{
    void (*function)(MyBaseClass* this_pointer);
};

It would be dangerous if that conversion worked as you would be able to call a function in class Child with a pointer or reference to a Parent which might not be of type Child.

If you are passing in a function and a pointer/reference you could use a boost::bind to achieve it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!