Dec 10, 2009

Declare C++ Interface

Weird C++ ! If your would like to declare an interface class, you shall use normal "class" keyword and declare every method to be "virtual". And you must add a virtual destructor, which must be implemented (as a blank method), otherwise you would not be able to pass an instance of the class as a pointer.

Here is the demo code: (from http://stackoverflow.com/questions/318064/how-do-you-declare-an-interface-in-c)

class IDemo
{
  public:
    virtual ~IDemo() {}
    virtual void OverrideMe() = 0;
};

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

class Child : public Parent, public IDemo
{
  public:
    virtual void OverrideMe()
    {
      //do stuff
    }
};