从驻留在非相邻内存中的实例继承

Inheritance from an instance residing in non adjacent memory

本文关键字:内存 实例 继承      更新时间:2023-10-16

我认为这是不可能的,但我应该问:

我有一个"物体",它在精神上是由头和尾组成的,它们一起构成了身体。头是数组的元素。

体体有一个成员,用于存储头的索引。

struct head{ body* bodyP; int kind; int execution_level;}
head heads[0x1000];
struct tail{ int head_index; other_data xxxx; }

结构(或类)"head"也有我需要从tail中访问的方法。

我想要的是有可能将"尾巴"的名称改为"身体"(容易的部分),并能够使"身体"从头部继承,只是通过使用head_index来引用头部。

您需要把它画出来,以显示常用的和可选的部分。

这是我对你的问题描述的理解(实现),而不是你的代码:

struct Body;  // Forward declaration.
struct Common_To_Head_Tail
{
  Body * p_body; // link to a body, hopefully it's same between head and tail.
  // Methods common to Head and Tail
};
struct Head
: public Common_To_Head_Tail
{
}
struct Tail
: public Common_To_Head_Tail
{
}
struct Body
{
  Head * p_head;
  Tail * p_tail;
};


头和尾有一些共同的东西,比如指向体的指针(如果愿意,可以使用索引)。前向声明是编译器解析Body指针所必需的。

HeadBody继承(共享)头部和身体的共同物质。

Body 包含一个头和一个尾。使用指针(索引)是因为部件可以位于其他地方。