3

Going through a blog about dependency injection. For the interface in their example, they use a struct with pure virtual methods instead of a class, and then inherit from the interface as a class.

struct IEngine
{
    virtual void Start() = 0;
    virtual void Stop() = 0;
    virtual ~IEngine() = default;
};

class V8Engine : public IEngine
{
    void Start() override { /* start the engine */ }
    void Stop() override { /* stop the engine */ }
};

My instinct would have been to make the IEngine interface a class instead. Is there a difference between using a struct vs class for the interface here? Would there be a situation where you would prefer to use one over the other?

ChrisMcJava
  • 2,145
  • 5
  • 25
  • 33

0 Answers0