等效于 C++ 中的 Java 接口? [英] Equivalent of Java interfaces in C++?

查看:33
本文介绍了等效于 C++ 中的 Java 接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何在 C++ 中声明接口?
c++中的java接口?

我是一名学习 C++ 的 Java 程序员,我想知道 C++ 中是否有类似 Java 接口的东西,即另一个类可以实现/扩展多个的类.谢谢.附言新来的,如果我做错了什么,请告诉我.

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.

推荐答案

在 C++ 中,一个只包含纯虚方法的类表示一个接口.

In C++ a class containing only pure virtual methods denotes an interface.

示例:

// 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.
    }
};

这篇关于等效于 C++ 中的 Java 接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆