C ++: explanation of this pointer

Source: Internet
Author: User

 

For the this pointer in C ++, we suggest you read this article, "this pointer in C ++" for your reference.

This pointer is a special pointer. When a non-static member function of the class is executed, this pointer exists. It points to an object of the class, and a member function of this object is being called.

The name of this pointer is always this and is always passed to every declared member function as an implicit parameter. For example:

In actual programming, the function declaration does not need to include this parameter.

 

When the program calls a member function of an object, the compiler adds the address of the object to the parameter list, as if the function adopts the Declaration shown above, it is called in the following way:

The static member function does not have the this pointer.

 

When calling a member function of an object, the compiler passes the object address to the this pointer and then calls the function. Therefore, you use the this pointer implicitly to call a member function.

1. Use this pointer as the return value

This pointer allows the member function to return the called object to the caller. In the previous program, the overload assignment operator does not return values. Therefore, you cannot assign values to strings in the following form:

In order to make the overloaded class assignment mechanism more convenient, the assignment function must return the assignment result, which is the target object here. When the value assignment function is executed, the returned value is exactly the content referred to by this pointer. The following program modifies the previous program so that the overload assignment operator returns a reference to a Date object.

 

2. Use the this pointer in the linked list

In an application, if the data structure contains members pointing to its own type, using this pointer will provide more convenience. The following program creates a ListEntry-like linked list.

When the program is running, a message is prompted to enter a string of names. After the input is complete, enter \ "end \". Then, the program displays all the names just entered in reverse order.

 

In the process, the ListEntry class contains a string and a pointer to the first table item. The constructor obtains the memory allocated to the string from the center, copies the content of the string to the memory, and sets the link pointer to NULL. The destructor releases the memory occupied by the string.

The member function PrevEntry () returns the pointer to the first table item in the linked list. Another member function displays the content of the current table item.

The member function AddEntry (), which copies the this pointer to the preventry pointer of the parameter, assigns the address of the current table item to the link pointer of the next table item, and constructs a linked list. It does not change the content of the listEntry object that calls it. It just assigns the object address to the preventry pointer of the ListEntry object referenced by the function parameter, although this function does not modify the object data, it is not a constant type. This is because it copies the content of the object address this pointer to a non-long constant object, the compiler considers that this extraordinary object may modify the data of the current object through the copied address. Therefore, the AddEntry () function does not need to use the const when declaring the object.

We hope that the above content will help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.