Wrapping linked lists in iterators

后端 未结 5 641
青春惊慌失措
青春惊慌失措 2021-01-20 01:21

A set of APIs that I commonly use follow a linked-list pattern:

struct SomeObject
{
    const char* some_value;
    const char* some_other_value;
    SomeObj         


        
5条回答
  •  遥遥无期
    2021-01-20 01:36

    I've seen some very good answers so far but I fear they might be "bare".

    Before blindingly charge in let's examine the requirements.

    • As you noticed the structure is similar to a singly linked list, the C++0x standard defines such a structure, called forward_list. Read up its interface, yours should come close.
    • The iterator type will be ForwardIterator.

    I would like to expose one very annoying fact though: who's responsible for the memory ?

    I am worried because there's no copy facility in the interface you've provided, so should we disable the copy constructor and assignment operator of our new list class ?

    Implementation is easy, there's enough on this page even though the iterator don't properly implement the iterator traits in general, but I would consider ditching this idea completely and move on to a better scheme:

    • class MyObject { public: ... private: SomeObject mData; };
    • Wrapping up GetObjectList and returning a deque, I guess the LONG returns the number of items ?

提交回复
热议问题