Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of this pointer of C++

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces C++ 's this pointer case analysis of the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone after reading this C++ this pointer example analysis article will have a harvest, let's take a look at it.

The this pointer is the address that exists in the member function of the class and points to the instance of the class in which the function is called. The this pointer of an object is not part of the object itself and does not affect the result of the sizeof (object). The this scope is within the class, and when the non-static member of the class is accessed in the non-static member function of the class, the compiler automatically passes the address of the object itself to the function as an implicit parameter.

First give a class in C++Primer directly, you may not be able to fully understand it, but don't worry, let's explain a little bit.

Class Sales_data {std::string isbn () const {return bookNo;} Sales_data& combine (const Sales_data&); double avg_price () const; std::string bookNo; unsigned untis_sold = 0; double revenue = 0;}; / / Sales_data non-member function interface Sales_data add (const Sales_data, const Sales_data&); std::ostream& print (std::ostream&, const Sales_data&) Std::istream& read (std::istream&, const Sales_data&)

All members of a class must be declared inside the class, but the body of the member function can be defined externally, such as the Sales_data class we wrote above, where the isbn function is defined internally, and the combine and avg_price functions are defined externally

The functions defined inside the class are implicit inline functions

The inline function, that is, the function that is expanded "inline" when called, that is, it is not called through the mechanism of the function call, but by inserting the function body directly into the call, such as the following call

Inline const string & cout

The concept of "this" #

Let's first look at the isbn function.

Std::string isbn () const {return bookNo;}

Its argument list is empty and returns a string object, so how does it know which class the string object is from?

This

Let's first look at an example of a call.

Total.isbn () when we call a member function, we are actually calling it on behalf of an object (total in this case). Isbn points to a member of Sales_data (bookNo), then it implicitly points to the member of the object that called the function.

In the total.isbn () call, when isbn returns bookNo, it actually implicitly returns total.bookNo

The member function isbn accesses the object that calls it through an additional implicit parameter called this (this is actually a pointer to the current object). When we call a member function, initializing the this,this with the object address of the function will point to the current object.

For example, when calling total.isbn (), the compiler is responsible for passing the address of total to the implicit parameter this of isbn, which can be equivalent to that the compiler rewrites the call in the following form

Std::string isbn () const {return this- > bookNo}

Because the purpose of this is always to point to "this" object, this is a constant pointer (this is a top-level const,this pointer itself is a constant)

Isbn () const#

First of all, you need to know the basic usage of const and how to distinguish the top-level cosnt from the underlying const. It is recommended to read this article first. Here are a few lines of code to help you recall the top-level cosnt.

Int I = 0intint * const p1 = & I; / p1 itself is constant, top constconst int ci = 42; / / ci itself is constant, top constconst int* p2 = & ci; / / * after const, p2 is the pointer to the constant, the bottom constconst int* const p3 = p2; / / first the left is the top layer, then the right is the bottom, and p3 is the constant pointer to the constant const int& r = ci / / declare that the const referenced is the underlying const,r, which is a reference to a constant.

Okay, let's get down to business.

Let's start with the conclusion: "the function of const in isbn () const is to modify the type of implicit this pointer."

First of all, let's forget about isbn. By default, the type of this is a constant pointer to a non-constant version of the class type (this is a top-level const,this pointer that itself is a constant, but the object it points to is not a constant). In the member function of Sales_data, the default type of this is Sales_data* const.

Although this is implicit, it also follows initialization rules, so by default we cannot bind this directly to a constant object, nor can we call ordinary function members on a constant object (need to use this)

Specifically, if, I mean, if isbn is an ordinary function without const,this, and the object referred to by this will not be changed in isbn (only bookNo is returned), then we should declare this as const Sales_data* const, so setting this to a pointer to a constant can increase flexibility.

However, this implicitly does not appear in the parameter list, so where does the this declaration point to a pointer to a constant? C++ 's practice is to allow the const keyword to be placed after the parameter list of the member function, which is what we see as isbn () const. The const immediately following the parameter list indicates that this is a pointer to a constant. A member function that uses const like this is often called a constant member function.

/ / the following code is illegal and is only used to illustrate how implicit this pointers are used, but we cannot explicitly define this pointers / / keep in mind that this here is a pointer to a constant, because isbn is a constant member

Std::string Sales_data::isbn (const Sales_data * const this) {return this- > isbn;}

Define a function that returns a this object #

We declared a combine function in Sales_data earlier.

Sales_data& combine (const Sales_data&)

Now let's define this function externally.

Sales_data& Sales_data::combine (const Sales_data& rhs) {untis_sold + = rhs.untis_sold; revenue + = rhs.revenue; return * this;}

Sales_data::combine uses the scope operator to illustrate that we define a function called combine, and the function is declared within the scope of the Sales_data class, so when combine uses untis_sold and revenue, it also implicitly uses members of Sales_data

When we call this combine

Total.combine (trans)

The address of total is bound to the implicit this parameter, while rhs is bound to trans

You should have noticed that the focus of this function should be on the return type and return statement.

Combine is designed to mimic the + = operator as much as possible. + = returns the left Operand as a left value. In order to be as consistent as possible, combine must return a reference type (in this case, the left Operand is a Sales_data object, so the return type is Sales_data&)

How to return, now we don't need to use an implicit this pointer to access a specific member of the function caller, but we need to access the object calling the function as a whole.

Return * this

The return statement dereferencing the this pointer gets the object that executes the function, and total.combine (trans) returns a reference to total.

This is the end of the article on "this pointer example Analysis of C++". Thank you for reading! I believe you all have a certain understanding of the knowledge of "C++ 's this pointer example analysis". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report