侵入性列表 [英] Intrusive list

查看:101
本文介绍了侵入性列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在创建一个侵入式列表(''next''和''prev''指针存储在一个对象中
在列表中)。一种方法是从包含这些指针的某个类继承

所有对象。但是,我的问题是不可接受的,因为它限制了

对象可以包含的列表数量。我必须在一个以上的列表中有一些对象。相反

的继承,我使用聚合:


class Node {//列表的节点

节点* prev,*下一个;

}


类SomeObject {//可以在3个独立列表中的对象

节点l1,l2,l3 ;

}


当我遍历我的列表时,我得到指向在SomeObject中存储的节点的指针。但我需要获得指向SomeObject本身的指针。现在

我正在使用不合格和丑陋的方式:


#define MEMBER_OFFSET(ClassName,FieldName)int(&(((ClassName

*)0) - > FieldName))

ListNode *节点;

SomeObject * so = reinterpret_cast< SomeObject *> ((char *)节点 -

MEMBER_OFFSET(SomeObject,l2));


我的问题是,是否有一种标准的符合方式?对于

示例使用指向成员的指示?


祝你好运,

Marcin


解决方案

Marcin Kalicinski写道:

我正在创建一个侵入性列表(''next''和'' prev''指针存储在列表中的对象中)。


为什么你不使用std :: list?

这样做的一种方法是从某些类继承
所有对象包含这些指针。但是,这对我的问题是不可接受的,因为它限制了
对象可以进入的列表数量1.


如何? C ++支持多重继承。

我必须在多个列表中包含一些对象。而不是继承,我使用聚合:

类节点{//列表节点
节点*上一页,*下一页;
}

class SomeObject {//可以在3个独立列表中的对象
节点l1,l2,l3;
}
当我遍历我的列表时,我得到指针到SomeObject中存储的节点。但我需要获得指向SomeObject本身的指针。现在
我正在使用不合格和丑陋的方式:

#define MEMBER_OFFSET(ClassName,FieldName)int(&((ClassName
*)0) - > ; FieldName))

ListNode *节点;
SomeObject * so = reinterpret_cast< SomeObject *>((char *)node -
MEMBER_OFFSET(SomeObject,l2));

我的问题是,有没有一种标准的符合方式呢?对于
示例使用指向成员的指针?




我不知道任何可移植的方式来获取对象的地址

任意子对象的地址。


Marcin Kalicinski写道:


我的问题是,有吗这样做的标准符合方式?对于
示例使用指向成员的指针?




编号

当然你可以从每个节点添加一个后退指针

类到它链接的对象。


class Node {

Node * prev,* next;

SomeObject * TheObject;

}


但老实说,我会更改设计。

声明一个容器为beeing主人和

拿着物品。所有其他列表只存储指向这些对象的
指针。这样每个对象

就可以被你想要的多个列表引用。


-

Karl Heinz Buchegger
kb******@gascad.at


< blockquote>

Uzytkownik" Jeff Schwab" < JE ****** @ comcast.net> napisal w wiadomosci

新闻:Gr ******************** @ comcast.com ...

Marcin Kalicinski写道:

为什么你不使用std :: list?




因为它是非侵入性的,我需要一个侵入性list。

这样做的一种方法是从包含这些指针的某个类继承
所有对象。但是,这对我的问题是不可接受的,因为它限制了
对象可以进入的列表数量。



如何? C ++支持多重继承。




但是我不能从同一个类继承多次。


Marcin

Hi everybody,

I am creating an intrusive list (''next'' and ''prev'' pointers are stored
within an object that is in a list). One method of doing that is to inherit
all objects from some class that contains these pointers. However, it is
unacceptable for my problem, because it limits the number of lists the
object can be in to 1. I must have some objects in more than 1 list. Instead
of inheriting, I use aggregation:

class Node { // Node of a list
Node *prev, *next;
}

class SomeObject { // Object that can be in 3 independent lists
Node l1, l2, l3;
}

When I iterate through my list, I get pointer to a Node that is stored
within SomeObject. But I need to get pointer to SomeObject itself. Right now
I''m using nonconforming and ugly way:

#define MEMBER_OFFSET(ClassName, FieldName) int(&(((ClassName
*)0)->FieldName))

ListNode *node;
SomeObject *so = reinterpret_cast<SomeObject *>((char *)node -
MEMBER_OFFSET(SomeObject, l2));

My question is, is there a standard conforming way of doing that? For
example by using pointers to members?

Best regards,
Marcin


解决方案

Marcin Kalicinski wrote:

I am creating an intrusive list (''next'' and ''prev'' pointers are stored
within an object that is in a list).
Why aren''t you using std::list?
One method of doing that is to inherit
all objects from some class that contains these pointers. However, it is
unacceptable for my problem, because it limits the number of lists the
object can be in to 1.
How so? C++ supports multiple inheritance.
I must have some objects in more than 1 list. Instead
of inheriting, I use aggregation:

class Node { // Node of a list
Node *prev, *next;
}

class SomeObject { // Object that can be in 3 independent lists
Node l1, l2, l3;
}

When I iterate through my list, I get pointer to a Node that is stored
within SomeObject. But I need to get pointer to SomeObject itself. Right now
I''m using nonconforming and ugly way:

#define MEMBER_OFFSET(ClassName, FieldName) int(&(((ClassName
*)0)->FieldName))

ListNode *node;
SomeObject *so = reinterpret_cast<SomeObject *>((char *)node -
MEMBER_OFFSET(SomeObject, l2));

My question is, is there a standard conforming way of doing that? For
example by using pointers to members?



I don''t know of any portable way to get the address of an object given
the address of an arbitrary sub-object.


Marcin Kalicinski wrote:


My question is, is there a standard conforming way of doing that? For
example by using pointers to members?



No.
But of course you could add a back pointer from each node
class to the object it links.

class Node {
Node *prev, *next;
SomeObject* TheObject;
}

But honestly, I would change the design.
Declare one container as beeing the master and
holding the objects. All other lists just store
pointers to those objects. This way each object
can be referenced by as many lists as you wish.

--
Karl Heinz Buchegger
kb******@gascad.at



Uzytkownik "Jeff Schwab" <je******@comcast.net> napisal w wiadomosci
news:Gr********************@comcast.com...

Marcin Kalicinski wrote:

Why aren''t you using std::list?



Because it is non-intrusive and I need an intrusive list.

One method of doing that is to inherit
all objects from some class that contains these pointers. However, it is
unacceptable for my problem, because it limits the number of lists the
object can be in to 1.



How so? C++ supports multiple inheritance.



But I cannot inherit multiple times from the same class.

Marcin


这篇关于侵入性列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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