Using reference as class members for dependencies

后端 未结 7 661
自闭症患者
自闭症患者 2020-12-13 01:06

I am going back to C++ after spending some time in memory-managed languages, and I\'m suddently kinda lost as to what is the best way to implement dependency injection. (I a

7条回答
  •  执笔经年
    2020-12-13 01:25

    It has been asked before, but my SO search skills are not up to finding it. To summarise my position - you should very rarely, if ever, use references as class members. Doing so causes all sorts of initialisation, assignment and copying problems. Instead, use a pointer or a value.

    Edit: Found one - this is a question with a variety of opinions as answers: Should I prefer pointers or references in member data?

提交回复
热议问题