Overloading the operator ++

I'm trying to write a doubly linked list for strings with some overloaded operators. I'm not familiar with templates so my list has two classes. One for the node and another for the list itself.

One of the features I'd like to include is an overloaded ++ and -- operator. My goal is to have these work much like the ++ and -- operators on iterators; they update the iterator to point to the next element in the container. However, my implementation isn't working as expected.

I've tried playing with the implementation of the overloaded operator++ by returning a pointer, a reference to a pointer, but neither of them are working as expected. I trimmed down my code to the only applicable 60ish lines and wrote a function called printContents() to exhibit the issue I'm having. When the operator ++ is called within printContents(), the the pointer, p_current_node is updated, but it does not point to the correct address. Could someone please explain why it's not pointing to the correct address and what steps need to be taken to correct it?

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 <string>
#include <iostream>

class Node{
public:
    Node() = default;
    Node(std::string name, Node* prev_node): data(name), p_prev_node(prev_node), p_next_node(nullptr){};

    Node* p_next_node;
    Node* p_prev_node;

    std::string data;

    Node* operator++(int){ return p_next_node; } //overloaded postfix, should this be returning a reference?
};

class LinkedList{
public:
    LinkedList(): p_root(nullptr), p_last(nullptr){};
    LinkedList(std::string data): p_root(new Node(data, nullptr)) { p_last = p_root; };

    void printContents();
    void push_back(std::string data);

    std::string& operator[](const size_t index);

private:
    Node* p_root;
    Node* p_last;
};

void LinkedList::push_back(std::string data) {
    if(p_root){
        p_last->p_next_node = new Node(data, p_last);
        p_last = p_last->p_next_node;
    }
    else{
        p_root = new Node(data, p_root);
        p_last = p_root;
    }
}
void LinkedList::printContents() {
    Node* p_current_node = p_root;
    
    std::cout << "The address of the root is: " << p_root << std::endl; //output "A"
    std::cout << "The address of the the 2nd node is: " << p_root->p_next_node << std::endl; //output "B"
    std::cout << "The address of the the 2nd node is: " << p_current_node->p_next_node << std::endl; // output "C"

    p_current_node++; //

    std::cout << "The address of the the 2nd node is: " << p_current_node << std::endl;// want this to be same address from output "C"
    std::cout << "The address of the root is: " << p_root << std::endl;
}

int main(){
    LinkedList first_names;

    first_names.push_back("John");
    first_names.push_back("Colin");
    
    first_names.printContents();

    return 0;
}
Last edited on
Regarding return types etc, cppreference has a page on operator overloading, take a look at how post/pre-increments are typically overloaded: http://en.cppreference.com/w/cpp/language/operators#Increment_and_decrement

When the operator ++ is called within printContents(), the the pointer, p_current_node is updated,
 
Node* operator++(int){ return p_next_node; } 


Nothing is updated by that operator++. It has to actually assign a new value to p_next_node.
Ok, I was able to follow some of that, but I still need some guidance.

Since my structure is a linked list, I want the operator ++ to advance to the next element in the list. Note, I'm not sure if I should be using the pre or post increment. Either way, I want the operator++ to update the object to point to the next element in the list.

For example, the following code iterates through each element of a linked list starting from the root until the last element.

1
2
3
4
5
6
    Node* p_current_node = p_root;

    while(p_current_node->p_next_node != nullptr){ //iterate through linked list from root
        p_current_node = p_current_node->p_next_node;
        
    }


I want to implement the same behavior within my overloaded operator, but I'm not sure how to assign a new value to it. I want to do something like:

Node*& operator++(int){ this = this->p_next_node; return this; }

but I don't think I can assign a new value to this.

Do you know how I would assign a value so I could update the Node pointer?


The members of your node are called p_prev_node and p_next_node, so to update the node, you would have to change those members: p_prev_node = p_next_node; p_next_node = p_next_node->p_next_node;, plus checks against null.

Another issue is that in main(), you're working with a pointer and not with any node, so you are not calling your operator++ to begin with. Pointers are not user-defined types and their operators cannot be overloaded.
Topic archived. No new replies allowed.