本文介绍了侵入性列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我正在创建一个侵入式列表(''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 解决方案 我不知道任何可移植的方式来获取对象的地址 任意子对象的地址。 编号 当然你可以从每个节点添加一个后退指针 类到它链接的对象。 class Node { Node * prev,* next; SomeObject * TheObject; } 但老实说,我会更改设计。 声明一个容器为beeing主人和 拿着物品。所有其他列表只存储指向这些对象的指针。这样每个对象 就可以被你想要的多个列表引用。 - Karl Heinz Buchegger kb******@gascad.at Uzytkownik" Jeff Schwab" < JE ****** @ comcast.net> napisal w wiadomosci 新闻:Gr ******************** @ comcast.com ... 因为它是非侵入性的,我需要一个侵入性list。 如何? C ++支持多重继承。 但是我不能从同一个类继承多次。 Marcin Hi everybody,I am creating an intrusive list (''next'' and ''prev'' pointers are storedwithin an object that is in a list). One method of doing that is to inheritall objects from some class that contains these pointers. However, it isunacceptable for my problem, because it limits the number of lists theobject can be in to 1. I must have some objects in more than 1 list. Insteadof inheriting, I use aggregation:class Node { // Node of a listNode *prev, *next;}class SomeObject { // Object that can be in 3 independent listsNode l1, l2, l3;}When I iterate through my list, I get pointer to a Node that is storedwithin SomeObject. But I need to get pointer to SomeObject itself. Right nowI''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? Forexample by using pointers to members?Best regards,Marcin 解决方案I don''t know of any portable way to get the address of an object giventhe address of an arbitrary sub-object.No.But of course you could add a back pointer from each nodeclass 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 andholding the objects. All other lists just storepointers to those objects. This way each objectcan be referenced by as many lists as you wish.--Karl Heinz Buchegger kb******@gascad.atBecause it is non-intrusive and I need an intrusive list. How so? C++ supports multiple inheritance.But I cannot inherit multiple times from the same class.Marcin 这篇关于侵入性列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!