linked list

Hi

I know what and how to make a linked list but I do not know how to make one with the pointer THIS.

Does someone could help me find a track on how to use THIS keyword in the management of a linked list of objects of the same nature?

Thank
Last edited on
Keyword this (not THIS) has nothing common with linked lists except that it can be used the same way as it can be used in non-static member functions of another classes.
Last edited on
How did you design a linked list without the pointer? From there we can explain how to do it with classes/the this pointer.
http://www.cplusplus.com/forum/beginner/48857/#msg265438

vince1027 wrote

in C++, the "this" keyword is a pointer to the current instance of the class in a function in which the "this" is used. Doing something like:

return (*this);


basically returns the instance of the class, "*" is the dereference operator and "this" is the pointer to the instance.



That being said, the "this" keyword is particularly useful when you have different classes that need to do the same thing. So instead of creating different functions for them, you can create a function that uses the "this" keyword to access and return the types or members of those classes


http://www.youtube.com/watch?v=WFqqX0MMn-w
Hi thank for response

I specify my question. Do you have an idea on how we can write a member function inserting a new object into a linked list of objects of the same nature? The "this" keyword is expected in this situation and is self-referential.

I think managing a linked list of objects of the same type to write a member function inserting a new object (supposedly transmitted argument), we must put the address in the previous object in the list.

I read about self-reference the this keyword. I confess I do not quite understand. I would have a concrete example or further explanation.
The this pointer is suppossed to help the user get the implicit address of the object.

You can always avoid this by replacing the member function with a friend function.
Here is a project I am working on that uses the this keyword:

1
2
3
4
5
6
7
8
9
10
11
12
13
for (int s = 0; s < sizeY; ++s)
  {
    //
    for (int w = 0; w < sizeX; ++w)
    {
      //
      area[w][s].GetBlock(f, toRow, w, s);
    }
    
    //
    
    area[0][s].PrintRow();
  }


1
2
3
4
5
6
7
8
9
void Grid::Block::PrintRow()
{
  for (vector<int>::iterator temp = this->Row.begin(); temp != this->Row.end(); ++temp)
  {
   cout << std::setw (2);
   cout << *temp << " ";
  }
  cout << endl;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void Grid::Block::GetBlock(char in, vector <int> &myVec, int row, int col)
{  
  switch(in)
  {
    case '.':
      this->SetBlock(1, row+1, col+1, true);
      myVec.push_back(1);
      break;
      
    case '*':
      this->SetBlock(0, 0, 0, false);
      myVec.push_back(0);
      break;
      
    default:
      this->SetBlock(in, row+1, col+1, false);
      myVec.push_back(in);
      break;
  }
}



In this example I have created 400 instances of the same class (prob not very efficient) and since I need to do the same thing with each and every instance, it will be more efficient to use the same function for all instances of that class; because I need all of them during run time, it will be easier to just call a member function which can handle and safely do the same task each time it is called, than creating different functions for each instace. This is where the 'this' keyword comes in handy. Because I could just call the member function just like any other member of that class and the function will use the 'this' keyword to identify what instance of the class called it and is able to carry out the required procedure for that instance

Another way I could have done this would have been to pass the class object as a parameter to the function that way, the function has direct access to the object itself, but when you compare that method to using the 'this' method, you will find the later to be easier to work with.

Hope that helps
Last edited on
Many thank for example and explication
Because I could just call the member function just like any other member of that class and the function will use the 'this' keyword to identify what instance of the class called it and is able to carry out the required procedure for that instance

The use of this in your function is completely superfluous. The procedure is called for this implicitly when the keyword is not used.

The usual use for this is to disambiguate identifiers or when you need the address of the invoking object, such as in the insertBefore method of the following class, which the OP may find relevant, given the subject of the thread.

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
class Node 
{
public:
    // ...
    void insertBefore(Node* node) ;
    // ...

private:
    int data ;
    Node * next ;
    Node * prev ;
};

// PRE: Invoking Node is not in a list, node is not null
// POST: Invoking node is in the same list as node, immediately preceding node.
// note: If there is a "head" pointer being maintained, the calling code is responsible
//          for maintaining it.
void Node::insertBefore( Node* node )
{
    next = node ;
    prev = node->prev ;
 
    node->prev = this ;
    if ( prev )
        prev->next = this ;
}


Smac89 wrote:
That being said, the "this" keyword is particularly useful when you have different classes that need to do the same thing. So instead of creating different functions for them, you can create a function that uses the "this" keyword to access and return the types or members of those classes
If you have different classes that need to do the same thing, you have a design flaw.
Smac89 wrote:
That being said, the "this" keyword is particularly useful when you have different classes that need to do the same thing. So instead of creating different functions for them, you can create a function that uses the "this" keyword to access and return the types or members of those classes
If you need to know the type of an object, you have a major design flaw.
Topic archived. No new replies allowed.