What is the practical use of pointers to member functions?

后端 未结 12 1508
醉酒成梦
醉酒成梦 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:55

    Imagine for a second you have a function that could call one of several different functions depending on parameters passed.

    You could use a giant if/else if statement
    You could use a switch statement
    Or you could use a table of function pointers (a jump table)

    If you have a lot of different options the jump table can be a much cleaner way of arranging your code ...

    Its down to personal preference though. Switch statement and jump table correspond to more or less the same compiled code anyway :)

提交回复
热议问题