What is the practical use of pointers to member functions?

后端 未结 12 1528
醉酒成梦
醉酒成梦 2020-12-03 08:13

I\'ve read through this article, and what I take from it is that when you want to call a pointer to a member function, you need an instance (either a pointer to one or a sta

12条回答
  •  悲哀的现实
    2020-12-03 08:37

    There are many practical uses. One that comes to my mind is as follows:

    Assume a core function such as below (suitably defined myfoo and MFN)

    void dosomething(myfoo &m, MFN f){   // m could also be passed by reference to 
                                         // const
       m.*f();
    }
    

    Such a function in the presence of pointer to member functions, becomes open for extension and closed for modification (OCP)

    Also refer to Safe bool idiom which smartly uses pointer to members.

提交回复
热议问题