6 ms·
But they're not building trees, that's how inheritance is mostly used today. After reading this, I'm thinking that intrusive lists is the one use of inheritanc
by codr7 1y ago
But they're not building trees, that's how inheritance is mostly used today.
After reading this, I'm thinking that intrusive lists is the one use of inheritance in C++ that makes any sense.
- josephg 1y agoI'd still generally prefer intrusive lists to be done via composition. I've seen plenty of intrusive lists where each item was a member of multiple lists at the same time - which is quite hard to do if you need to inherit from an intrusive list element superclass.
- codr7 1y agometoo, but how do you pull that off in C++? How do you get back from node to containing value? Multiple inheritance, possible but you'd have to jump some hoops to disambiguate since you're dealing with multiple copies of the same base class.
- josephg 1y agoComposition. Sorry about the syntax - its been awhile since I wrote C++. class<T> IntrusiveListNode { T* next, T* prev } class SomeObj { IntrusiveListNode<SomeObj> list_foo; IntrusiveListNode<SomeObj> list_bar; }
- codr7 1y agoAh, of course; templates and linking the full object. Thanks for the update, my C++ is pretty rusty. But it brings up another problem for me; when iterating a list like that, how do you know which of the links to follow? I get that you can do it manually step by step, but if you wanted to say write an iterator. Member pointers?