I return to C ++ after spending some time in memory-driven languages, and suddenly get lost in what is the best way to implement dependency injection. (I am completely sold by DI because I found that this is the easiest way to make the test driven design very simple).
Now browsing SO and Google has brought me quite a few opinions on this, and I'm a little confused.
In response to this question, Dependency injection in C ++ , someone suggested you not pass raw pointers even for dependency injection. I understand that this is related to ownership of objects.
Now, ownership of objects is also discussed (although not in sufficient detail in my condition;)) in the infamous Google style guide: http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Smart_Pointers
So I understand that in order to clarify which object owns some other objects, you should avoid passing raw pointers. In particular, it seems that against this kind of encoding:
class Addict {
If Dependency is a purely virtual class (called the Poor Interface), then this code makes it easier to implement a dummy version of Dependency (using something like google mock).
The problem is that I really don't see the problems that I may encounter with such code, and why should I want to use something else besides raw pointers! Is it not clear where the addiction came from?
In addition, I read quite a few posts hinting that links should really be used in this situation, so is this code better?
class Addict {
But then I get other equally authoritative tips against using links as a member: http://billharlan.com/pub/papers/Managing_Cpp_Objects.html
As you can see, I'm not quite sure about the relative pros and cons of the various approaches, so I'm a little confused. I am sorry if this was discussed until death, or if it is only a matter of personal choice and consistency within this project ... but any idea is welcome.
Summary of responses
(I donโt know if this is a good SO-ticket for this, but I will add an example code for what I have compiled from the answers ...)
Of the various answers, here is what I will probably end up doing in my case:
- pass dependencies as a reference (at least to make sure NULL is not possible)
- in the general case, when copying is not possible, explicitly prohibit it and save the dependencies as a link
- in the rarer case when copying is possible, save the dependencies as RAW pointers
- let the creator of dependencies (of some factory) choose between stack allocation or dynamic allocation (and in the latter case, control via a smart pointer)
- establish an agreement on the separation of dependencies from own resources
So, I would like to get something like:
class NonCopyableAddict { Dependency & dep_dependency_;
And for the copyable class:
class CopyableAddict { Dependency * dep_dependency_; public:
From what I understand, there is no way to express the intention โI have a pointer to some things, but I donโt own it,โ which the compiler can use. So I have to resort to a naming convention here ...
Stored for reference.
As Martin pointed out, the following example does not solve the problem.
Or, if I have a copy constructor, something like:
class Addict { Dependency dependency_; public: Addict(const Dependency & dependency) : dependency_(dependency) { } ~Addict() {