Does using callbacks in C++ increase coupling?

前端 未结 8 1850
予麋鹿
予麋鹿 2021-01-03 11:04

Q1. Why are callback functions used?

Q2. Are callbacks evil? Fun for those who know, for others a nightmare.

Q3. Any alternative to

8条回答
  •  佛祖请我去吃肉
    2021-01-03 11:32

    1. Callbacks decrease coupling as they allow you to write code that calls a function that can be changed
    2. Callbacks are not evil, just that if you are using raw function pointers things can get messy really fast.
    3. There isn't an alternative to callbacks per se, but there are alternatives to using raw function pointers.

    A post pointing to Boost was made earlier for Boost.Function. If you are looking for a more generic solution for callbacks, such as multiple functions attached to the same callback or something like that, consider using Boost.Signals. The name comes from signals and slots, which is the way some people refer to callbacks nowadays, especially for GUIs.

提交回复
热议问题