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

How to understand C++this pointer

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

C++this pointer how to understand, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Again, the Stock stock class, suppose we implement a method that compares the current stock with the incoming stock and returns the high-priced stock. When we realize it, we will encounter some problems.

Const Stock & Stock::topVal (const Stock & s) const {if (s.total_val > total_val) {return s;} else {return?

There are some problems in this code, let's talk about it one by one.

First of all, when it comes to the function signature, there is no problem before, but the problem occurs in the const at the end of the signature. This is the first time we have seen const at the end of a function. This usage can only be used in member functions of a class or structure, not in ordinary functions.

It means that this function does not modify the value of any member variables, and the function with const at the end is called a constant member function.

Constant functions have some special properties:

Can be called by any function, only constant functions can be called

Can be called by any object, but const objects can only call const functions

The nature is not very complex, but it is somewhat like a tongue twister, which needs to be understood from the point of view of the nature of the const constant.

The second problem in the code is a bunch of question marks, where we want to return the current object, but we don't know how to represent it. To solve this problem, C++ has created a special pointer called this, which is used to point to the object that calls the member function, that is, the current object.

So with this, this code can be written as follows:

Const Stock & Stock::topVal (const Stock & s) const {if (s.total_val > total_val) {return s;} else {return * this}}

In addition, the this pointer can also be used to access member variables in the current object. For example, if we want to access the current total_val, we can write it as this- > total_val. It is essentially the same for us to use total_val directly, and the compiler optimizes it for us.

After reading the above, have you mastered the method of how to understand the C++this pointer? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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