C ++ pointer usage method

xiaoxiao2021-03-05  28

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 **.

That is to say this function modifies or may modify the caller's pointer, and the caller is transmitted like a normal variable, and does not use address operatures. As for what is going to use this method, I will say, very few. MFC uses it in its set class - for example, Coblist, which is a list of COBJECTS pointers. Class cobject: public cobject {... // Get / modify the element cobject * & getat (position position); cobject * getat (position position) const;}; there are two Getat functions, feature is giving given Position element. What is the difference? The difference is that one of the objects in the list, the other is not. So if you write it to the following: cobject * pobj = myList.get (POS); Pobj is a pointer to an object in the list, if you then change the value of pobj: POBJ = psomeotherobj; this is not able to change the object at position POS Address, but only changes the variable POBJ. However, if you write it as this: cobject * & rpobj = myList.get (POS); Now rpobj is a pointer to the object in a list, so when changing rpobj, you will change the object address in the list POS in the list. - In other words, this object is replaced. This is why Coblist will have two Getat functions. A value that can modify the pointer and the other cannot be. Note that I said here is a pointer, not an object itself. Both functions can modify the object, but only * & version can replace the object. Quote is important in C / C , and it is also an efficient processing means. So I want to be a C / C master, and there is no thorough understanding and skilled application for reference.

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

New Post(0)