Dependency injection in C++

前端 未结 8 1713
囚心锁ツ
囚心锁ツ 2020-12-13 04:40

This is also a question that I asked in a comment in one of Miško Hevery\'s google talks that was dealing with dependency injection but it got buried in the comments.

<
8条回答
  •  情书的邮戳
    2020-12-13 05:20

    Use RAII.

    Handing a raw pointer to someone is the same as handing them ownership. If that's not what you want to do, you should give them some kind of facade that also knows how to clean up the object in question.

    shared_ptr<> can do this; the second argument of its constructor can be a function object that knows how to delete the object.

提交回复
热议问题