Interface
推荐先阅读The-interface-principle.md
,以获得对C++中interface的全面了解: C++中,interface不仅仅局限于OOP,它的ADL,使得interface可以拓展到非method。
CppCoreGuidelines I: Interfaces
stackoverflow How do you declare an interface in C++?
A
Make a class with pure virtual methods. Use the interface by creating another class that overrides those virtual methods.
A pure virtual method is a class method that is defined as virtual and assigned to 0.
class IDemo
{
public:
virtual ~IDemo() {} //将此函数声明成了
virtual void OverrideMe() = 0;
};
class Child : public IDemo
{
public:
virtual void OverrideMe()
{
//do stuff
}
};
A
To expand on the answer by bradtgmurray, you may want to make one exception(例外) to the pure virtual method list of your interface by adding a virtual destructor. This allows you to pass pointer ownership to another party without exposing the concrete derived class(这句话解释了将destructor声明为virtual
和inline
的目的:这允许您将指针所有权传递给另一方,而不会暴露具体的派生类). The destructor doesn't have to do anything, because the interface doesn't have any concrete members. It might seem contradictory(自相矛盾) to define a function as both virtual
and inline
, but trust me - it isn't.
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
}
};
You don't have to include a body for the virtual destructor(因为它是inline
的) - it turns out some compilers have trouble optimizing an empty destructor and you're better off using the default.