更新时间:2022-10-23 13:36:30
在C ++中,只包含纯虚拟方法的类表示一个接口。 / p>
示例:
//定义Serializable接口。
class Serializable {
//如果对象可能是虚拟析构函数
//通过指向可序列化的指针来删除
virtual〜Serializable(){}
virtual std :: string serialize()const = 0;
};
//实现Serializable接口
class MyClass:public MyBaseClass,public virtual Serializable {
virtual std :: string serialize()const {
//实现去这里。
}
};
Possible Duplicate:
How do you declare an interface in C++?
Interface as in java in c++?
I am a Java programmer learning C++, and I was wondering if there is something like Java interfaces in C++, i.e. classes that another class can implement/extend more than one of. Thanks. p.s. New here so tell me if I did anything wrong.
In C++ a class containing only pure virtual methods denotes an interface.
Example:
// Define the Serializable interface.
class Serializable {
// virtual destructor is required if the object may
// be deleted through a pointer to Serializable
virtual ~Serializable() {}
virtual std::string serialize() const = 0;
};
// Implements the Serializable interface
class MyClass : public MyBaseClass, public virtual Serializable {
virtual std::string serialize() const {
// Implementation goes here.
}
};