Dependency Injection tool for c++ I'm trying to create some tool for easier dependency injection in c++14, here is one of my concepts using std::tuple.

template &lt;class... Types&gt;
class injector
{
public:
    injector(Types... i) { injected_ = std::tuple&lt;Types...&gt;(i...); }

protected:
    template &lt;class T&gt;
    T *injected()
    {
        return std::get&lt;T *&gt;(injected_);
    }

private:
    std::tuple&lt;Types...&gt; injected_;
};


using component_injector = injector&lt;scene *, entity *, foo *&gt;;
class component : component_injector
{
public:
    component(component_injector injected) : component_injector(injected) {};
    virtual ~component(){};

    void method()
    {
        injected&lt;scene&gt;()-&gt;method();
        injected&lt;foo&gt;()-&gt;method();
    }
};


int main(int argc, char *argv[])
{
    scene s;
    entity e;
    foo f;

    component c({&amp;s, &amp;e, &amp;f});
    c.method();

    return 0;
}


Is it good idea to create dependence this way? It has some limitations, for example I cannot have two dependencies of the same type etc. but code of useful part (component class) seems to be much clearer.
The injected types should be replaced by interfaces but the real types are used directly here for simplicity.
I don't think that makes it easier (or cleaner)
You are injecting pointers (which begs to ask ownership)
You should be preferring encapsulation over inheritance.


If I was doing it normally:

class component
{
    Scene&amp;   scene;
    Entity&amp;  entity;
    Foo&amp;     foo;
    public:
    component(Scene&amp; s, Entity&amp; e, Foo&amp; f)
        : scene(s)
        , entity(e)
        , foo(f)
    {}
    virtual ~component(){};

    void method()
    {
        scene-&gt;method();
        foo-&gt;method();
    }
};

int main()
{
    scene s;
    entity e;
    foo f;
    component c(s, e, f});

    c.method();
}


This seems much cleaner than your method (where you introduce another level of inheritance).