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.
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
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.
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.
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.
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 ;
}
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.