Inherited STL list iterator and overloading * and ->

I'm inheriting an STL list iterator and overloading operator* and operator-> to hide the link variable in my Node struct.

I'm not familiar with overloading operator* and operator-> and online searches have resulted in very limited information/examples about them.

Any advice or corrections would be appreciated. Right now, what I have written seems to be working fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <list>
#include <iostream>

template<typename T>
class My_List
{
  private: struct Node
           { 
             T data;
             Node* link; //"hide" this via iterator
             Node(T _data): data(_data) {};
           };
           
	   typedef std::list<Node> Node_List;
           Node_List list_;
  
  public:  struct iterator : public Node_List::iterator
           {
             iterator(const typename Node_List::iterator& _it);
             T& operator*() const;  //return Node::data, "hide" Node::link
             T* operator->() const; //return &Node::data, "hide" Node::link
           };
	         	   
           iterator begin()                { return list_.begin();          }
           void push_back(const T& _value) { list_.push_back(Node(_value)); } 
};

template<typename T>
My_List<T>::iterator::iterator(const typename Node_List::iterator& _it)
: Node_List::iterator(_it) {}

template<typename T>
T& My_List<T>::iterator::operator*() const
{
  return this->Node_List::iterator::operator*().data;
}

template<typename T>
T* My_List<T>::iterator::operator->() const
{ 
  return &this->Node_List::iterator::operator*().data;
}

struct My_Struct
{
  unsigned int id;
  unsigned int data;
};

int main()
{
  My_List<int> list0; 
  list0.push_back(50);
  My_List<int>::iterator it0 = list0.begin();
  std::cout << *it0 << std::endl; //50

  My_List<My_Struct> list1;
  My_Struct value = { 20, 0 };
  list1.push_back(value);
  My_List<My_Struct>::iterator it1 = list1.begin();
  std::cout << it1->id << "::" << it1->data << std::endl; //20::0
  
  return 0;
}
Wait, what's the point of Node? In fact, what's the point of My_List? It's an ultra-thin wrapper around std::list that only removes functionality.
My_List will contain a lot more variables than just Node_List list_. There will be a Root_List* root_, Sync_List* link_, and unsigned int id_.

I have removed the majority of my code to keep it simple. Node_List list_ in the OP will actually be Root_List* root_ and the Node_List will actually be a std::list<Root_List::iterator>.

My_List is actually called Sync_List. Sync_Lists can be linked to other Sync_Lists via their copy ctor and assignment operator(shallow copies). Each call to push_back() and erase() will be synchronized. Ex:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "Sync_List.h"

int main()
{
  Sync_List<int> list1;
  Sync_List<int> list2 = list1; //list2 synced with list1
  Sync_List<int> list3;

  list3 = list2; //list 3 synced with list 2 and list 1

  list2.push_back(50);
  list3.push_back(40);
  list1.push_back(20);

  std::cout << "list1: " << list1 << std::endl; //overloaded operator<<
  std::cout << "list2: " << list2 << std::endl;
  std::cout << "list3: " << list3 << std::endl;

  return 0;
}


output
list1: 50 40 20
list2: 50 40 20
list3: 50 40 20


Some functions calls will affect all synced lists and others functions will only affect the list its being called by:

void push_back(T& _value); //synchronized
erase(iterator _e); //synchronized
remove(iterator _e); //non-synchronized
swap(iterator _e0, iterator _e1); //non-synchronized
duplicate(iterator _e, iterator _pos); //non-synchronized[/code]
Last edited on
Guess I just didn't understand what I was trying to do. I need to implement my own iterators. Found couple places to start:

http://msdn.microsoft.com/en-us/magazine/cc301955.aspx
http://www.cplusplus.com/reference/std/iterator/iterator
Topic archived. No new replies allowed.