Why do people do use "this" a lot in classes for linked lists?

Please Note: This is for a homework assignment, however, I'm not asking you to do it for me. Instead, I'm asking for clarification on what "this" means and why people use it frequently.


Okay, I've recently finished taking notes from this page for learning about Single Linked Lists:
https://www.codementor.io/codementorteam/a-comprehensive-guide-to-implementation-of-singly-linked-list-using-c_plus_plus-ondlm5azr

And it never used "this" once.


Then I look a look at my teacher's notes and I don't understand two things (which probably go hand in hand :P).
1. I don't really know what "this" does.
2. I don't see the reason for having "this" stated so much. In the example problem I did (from the link above), "this" was never used and it worked perfectly.

Here's an example of my professor's code:
1
2
3
4
5
6
7
8
9
10
11
12
        // adds an element to the end (insert end)
        void push_back(int data) {
            Int_Node * node = new Int_Node;
            node->data = 0;
            node->next = NULL;
            
            this->tail->data = data; // update tail node's data
            this->tail->next = node; // link new node at the end
            this->tail = node; // make new node the tail node
            
            this->nodeCount++;
        }

You can find the complete code here: https://github.com/rambasnet/cs2notebooks/blob/master/LinkedLists.ipynb
Scroll down to "Linked List Implementation as ADT" for the code.


Help would be greatly appreciated, thanks!
Lets write a struct and a standalone function:
1
2
3
4
5
6
7
8
9
10
11
12
13
struct List {
  Node* tail;
  int count;
};

void push_back( List* list, int data ) {
  // code ...

  list->tail->data = data;
  list->tail->next = node;
  list->tail = node;
  list->count++;
}

and use them:
1
2
3
4
int main {
  List one;
  push_back( &one, 42 );
}


Then change the standalone function into a member:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct List {
  Node* tail;
  int count;
  void push_back( int data );
};

void List::push_back( int data ) {
  // code ...

  this->tail->data = data;
  this->tail->next = node;
  this->tail = node;
  this->count++;
}

and use:
1
2
3
4
int main {
  List one;
  one.push_back( 42 ); // during this function call the 'this' points to one
}

Do you notice similarities and differences?


The this is a pointer that points to the object, whose member was called.
The this is automatically created and set for each member function.

You do need to use the this, if there is name ambiguity.
1
2
3
4
5
6
struct Example {
  int data;
  bool test( int data ) {
    return this->data == data;
  }
};

The function wants to use both the member 'data' of Example and the function parameter 'data'. Inside the function the parameter name hides (masks) the member that has same name, but with this we can differentiate them. (It would be easier to choose names differently here, but not always.)


Your professor's code is unlikely to have name masking. She probably wants to emphasize that tail and nodeCount are member variables of the class. The code is bit more verbose, but also more explicit and self-documenting. That is a style choice. Optional.
keskiverto wrote:
Lets write a struct and a standalone function:

.....

Ah okay, thank you!
Topic archived. No new replies allowed.