4.4 Making use of WhereTo and ThisIsA

WhereTo can go where a pointer is needed, and ThisIsA is never used explicitly.

Listing 4 is an example of how we can use the WhereIs type.


Listing 4:LinkedList.01
 
1#include <iostream> 
2#include "whereis03.cc" 
3 
4using namespace std; 
5 
6template <class T> class List 
7{ 
8    class Node 
9    { 
10        T payload;  
11        WhereIs<Node> next;  
12      public: 
13        void setNext(WhereIs<Node> &next) 
14        { 
15          next.detach();  
16          next.becomesAliasOf(next); 
17        } 
18        WhereIs<Node> getNext(void) const { return next; } 
19        Node(const T &v):payload(v) 
20        { 
21        } 
22        Node(const Node &n):payload(n.payload),next(n.next) 
23        { 
24        } 
25    }; 
26    WhereIs<Node> first;  
27  public: 
28    void insert(const T &v) 
29    { 
30      Node n(v); 
31      WhereIs<Node> oldFirst; 
32      oldFirst.becomesAliasOf(first); 
33      first.detach();  
34      first.allocate(n); 
35      (*first).setNext(oldFirst); 
36    } 
37    unsigned count(void) const 
38    { 
39      WhereIs<Node> tmp; 
40      tmp.becomesAliasOf(first); 
41      while (!tmp.isNull()) 
42      { 
43        tmp.detach(); 
44        tmp.becomesAliasOf((*tmp).getNext()); 
45      } 
46    } 
47}; 
48 
49int main(void) 
50{ 
51  List<int> l; 
52 
53  l.insert(12); 
54  l.insert(23); 
55  cout << l.count(); 
56  return 0; 
57}

  4.4.1 Node
  4.4.2 First