Quantcast
Viewing latest article 10
Browse Latest Browse All 10

C++ Kana #2 — Dependency Injection

This time we'll exercise templates and the typeid keyword to build a Guice-inspired dependency injection framework. Write class Injector that allows you to write


class IService {
public:
    virtual ~IService() {};

    virtual void serve() = 0;
};

class HelloService : public IService {
public:
    void serve() {
        std::cout <<"Hello, world!"<< std::endl;
    }
};

int main() {
    Injector injector;

    injector.bind<IService>().to<HelloService>();

    IService* service = injector.newInstance<IService>();
    service->serve();
    delete service;

    return 0;
}

Viewing latest article 10
Browse Latest Browse All 10

Trending Articles