Q:

Do I need a framework to use dependency injection?

I want to start practising dependency injection in C++ to make my code more testable, however after some googling I found articles which say I must use some framework for dependency injection. Is it so? Why does one need such a framework? Why can't I inject dependencies either by subclassing and passing dependencies in constructor by reference or pointer or by passing the class I want to use as dependency as template parameter?

A:

Dependency Injection is a software design pattern which allows you to avoid dependencies to concrete classes. A Dependency container has the responsibility of object creation and to pass/inject dependencies into dependent objects. If you use a DI container your application delegates the responsibility of composing your object graph to a separate service. It provides the infrastructure to create your application in a consistent way and it forces conventions (especially interesting when you want to reuse it in different projects/teams and to force a consistent way to construct the object graph).
Poor-man's DI (e.g. constructor injection: passing a parameter by reference to constructor) is a valid way to do dependency injection. 
// Edit: A DI-container also manages the lifecycle of your objects/services. 

