Why use and reprint at the same time and & symbols (C ++)

xiaoxiao2021-03-06  21

In the following functions declaration, why should I use * and & symbols at the same time? And what happens to use this declaration? Void Func1 (MyClass * & PbuildingeElement); some people often ask such a problem in the forum. This article tries to explain this problem through some practical pointers. Take a closer look at this way of declaration, it is really a bit confusing. In a sense, "*" and "&" are two things that mean relatively, what is the meaning of what they put together? . In order to understand this approach to the pointer, let's review the concept of omnipotent pointer in C / C programming. We all know the meaning of myclass *: pointing to a pointer to an object, the type of this object is MyClass. Void Func1 (MyClass * PMYCLASS; //, for example: myclass * p = new myclass; func1 (p); this processing method of this code is going to use, create a Myclass object, then incoming it into FUNC1 function. Now suppose this function wants to modify PMYCLASS: Void Func1 (MyClass * PMYCLASS) {DOSMETHING (PMYCLASS); PMYCLASS = // Other objects} The second statement only modifies the value of PMYCLASS during the function process. There is no value to modify the value of the caller P. If P pointing to an object located at address 0x008A00, when FUNC1 returns, it still points to this particular object. (Unless FUNC1 has bugs to mess with the pile, there is a possibility.) Now, it is now assumed that you want to modify the value P in FUNC1. It is your right. The caller passed into a pointer, then the function assigns this pointer. In the past, it is generally passing double pointers, namely pointers, for example, cmyclass **. Myclass * p = null; func1 (& p); void func1 (MyClass ** PMYCLASS); {* PMYCLASS = new myclass; ...} After calling FUNC1, p point to new objects. In COM programming, you will encounter such a usage - for example in the queryinterface function of the query object interface: Interface isomeinterface {HRESULT Queryinterface (IID & IID, Void ** PPVOBJ); ...}; lpsomeinterface P = null; POB -> Queryinterface (IID_SOMEINTERFACE, & P); here, P is a pointer of the SomeInterface type, so & P is a pointer of the pointer. When the queryinterface returns, if the call is successful, the variable P contains a pointer to the new interface. If you understand the pointer of the pointer, you will definitely understand the pointer reference, because they are completely. If you declare the function below: Void func1 (MyClass * & PMYClass); {PMYCLASS = New Myclass; ...... In fact, it is a code of a pointer, but the syntax is different. Do not pass the address & p of P itself when it passes, but directly P itself: myclass * p = null; func1 (p); after the call, P point to a new object. Generally speaking, the principle of reference is more or less like a pointer, and it is a normal variable from the syntax. So as long as you meet * &, you should think of **.

转载请注明原文地址:https://www.9cbs.com/read-41468.html

New Post(0)