returning a reference

Hey guys sorry about the influx of questions today,this is my third I have been studying all day

anyway one thing that has always kind of confused me is when we return references,now I know passing by reference is pretty much passing the memory address of the object or type itself into a function but returning it seems to be giving me some issues

in my code below I have implemented a doubly linked list,the focus is on the overloaded pre and post fix operators

how come this code won't compile

1
2
3
4
5
6
7

 Node& operator++(){

     now = now->next;
     return now;
   }


now is a pointer to a node so it stores the memory address of a node

shouldn't this be valid since we are returning an address of a node?

1
2
3
4
5
6
7

 Node& operator++(){

     now = now->next;
     return *now;
   }


this code seems to work fine but here we are returning the object itself right? I thought we should be just returning the address?

thanks for bearing with me

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
 #include <iostream>

using namespace std;


class Node{

   public:
       int number;
       Node* next;
       Node* previous;

       Node(int n,Node* ne,Node* p){

           number = n;
           next = ne;
           previous = p;

       }

};


class List{

public:


   Node* head = NULL;
   Node* tail = NULL;
   Node* now;


   List(){}

   void addNode(int number){

      Node* newNode = new Node(number,head,NULL);

      if(head != NULL){

        head->previous = newNode;
      }

      head = newNode;
      now = head;

      if(tail == NULL){

        tail = newNode;
      }
   }

   void printList(){

      Node* current = head;

      while(current != NULL){

        cout << current->number << endl;
        current = current->next;

      }
   }

   void reverseList(){

    Node* current = tail;

    while(current != NULL){

        cout << current->number << endl;
        current = current->previous;
    }

   }

   Node& operator++(){

     now = now->next;
     return *now;
   }

   Node operator++(int){


       Node* temp = now;
       now = now->next;
       return *temp;
   }

};

int main()
{

    List list;
    list.addNode(1);
    list.addNode(2);
    list.addNode(3);

    list.reverseList();

}
Address is not reference.


What do you actually want to return?

* A reference to the pointer 'now', through which the caller can modify the value of 'now' of the List, if they so please.

* A reference to the Node that the pointer 'now' points to.
What if the 'now' did point to the last Node before the ++ ? You would return a reference to NULL.

* Pointer to the same node as the 'now' points to.

* Value of the node as the 'now' points to.
Topic archived. No new replies allowed.