Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;为API中定义的结构创建超类_C++_Api_Struct_Interface_Superclass - Fatal编程技术网

C++ C++;为API中定义的结构创建超类

C++ C++;为API中定义的结构创建超类,c++,api,struct,interface,superclass,C++,Api,Struct,Interface,Superclass,我有一个接口的API,我不能改变。 接口包含两个结构。由于它们非常相似,我希望将这两个结构的实例存储在同一个集合中 据我所知,我需要为这些结构创建一个超类,以便将这两个结构的实例都添加到集合中。但是,不改变接口本身是否可能?< p>您不能在C++中创建类的超类,但是可以使用将两个结构都添加到集合中。 或者,如果您希望以相同方式访问每个结构中的某些变量/方法,则可以创建一个封装这两个结构的新类: // structs from the API struct A { int some_in

我有一个接口的API,我不能改变。 接口包含两个结构。由于它们非常相似,我希望将这两个结构的实例存储在同一个集合中


据我所知,我需要为这些结构创建一个超类,以便将这两个结构的实例都添加到集合中。但是,不改变接口本身是否可能?

< p>您不能在C++中创建类的超类,但是可以使用将两个结构都添加到集合中。

或者,如果您希望以相同方式访问每个结构中的某些变量/方法,则可以创建一个封装这两个结构的新类:

// structs from the API

struct A {
    int some_int;
    string some_string;
};

struct B {
    int some_int;
    string some_string;
}

// new struct that you write
struct ABInterface {
    int some_int;
    string some_string;

    // "copy" constructor from A object
    ABInterface(A a) {
        some_int = a.some_int;
        some_string = a.some_string;
    }

    // "copy" constructor from B object
    ABInterface(B b) {
        some_int = b.some_int;
        some_string = b.some_string;
    }
}

不清楚你在问什么。请在您的帖子中澄清。你需要更具体一点。C++没有超类的概念。简短的答案是“否”,除非添加一个超类到<代码>结构> <代码> s,不考虑改变接口。