Why would one use function pointers to member method in C++?

前端 未结 10 2154
情书的邮戳
情书的邮戳 2021-02-10 23:48

A lot of C++ books and tutorials explain how to do this, but I haven\'t seen one that gives a convincing reason to choose to do this.

I understand very well why functio

10条回答
  •  执笔经年
    2021-02-11 00:14

    I have some code I'm working on right now where I used them to implement a state machine. The dereferenced member functions implement the states, but since they are all in the class they get to share a certian amount of data that is global to the entire state machine. That would have been tough to accomplish with normal (non-member) function pointers.

    I'm still undecided on if this is a good way to implement a state machine though.

提交回复
热议问题