In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to quote the secondary pointer and pointer of C++". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to reference the secondary pointer and pointer of C++".
Purpose: the parameter pointer changes the object (that is, address) pointed to by the argument pointer.
For example: the pointer pointer points to the white duck whiteDuck. If you want to design a function, this function can make the pointer point to the black duck blackDuck.
The figure is as follows:
The code is simplified as follows:
Duck * duck = & oldDuck;someFunction (duck); / / this cannot be achieved when duck points to newDuck;1 and shape participating arguments are pointers.
At this point, both point to the same address and can operate on the same object. However, the parameter pointer cannot change the address that the argument pointer points to, because the two are two completely unrelated pointer objects. When the function passes parameters, the value of the argument pointer is passed to the parameter pointer, which is a process of passing values.
2. Solution-pointer reference or secondary pointer
In order for the function to change the address pointed to by the argument pointer, you need to pass the address of the argument into the function.
There are two strategies to achieve this:
The parameter is a secondary pointer * * p
The formal parameter is a pointer reference * & p
The following two ways are introduced, and the corresponding code is provided.
The definition of Duck class is as follows:
Class Duck {public: Duck (int color) {this- > color = color;} / / 0 for white, 1 for black int color = 0;}; (1) formal parameter is secondary pointer * * p
At this point, the argument is the address of the pointer, and the parameter is the pointer of the pointer, that is, the parameter points to the argument. The parameter operates on the argument, not on the address that the argument points to.
Void changeObject (Duck * * p) {* p = new Duck (1);} / / call mode changeObject (& pointer); (2) formal parameter is pointer reference * & p
A pointer reference is a reference to a pointer, which also operates on the pointer, not on the address the pointer points to:
Void changeObject (Duck * & p) {p = new Duck (1);} / / call method changeObject (pointer). At this point, I believe you have a deeper understanding of "how to refer to the secondary pointer and pointer of C++". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.