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 * & PbuildingeLement); Some people often asked such problems 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 pass it into the 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. 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 the new object. In COM programming, you will encounter such uses everywhere -, 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 to 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, the pointer of the pointer to the front is a code, but the grammar is different. Do not pass the address & p of P itself in the transmission, but directly P itself: myclass * p = null; func1 (p);

After the call, P pointing 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 Coblist: public cobject {... // Get / modify Elements of the specified location CObject * & getat (position position); cobject * getat (position position) const;};

There are two Getat functions here, and the functions are elements that get a given position. What is the difference? The difference is that one of the objects in the list, the other is not. So if you are writing: COBJECT * POBJ = MyList.get (POS);

Then pobj is a pointer to an object in the list, and then change the value of Pobj: POBJ = psomeotherobj;

This is changed to the object address at the location POS, but only changes the variable POBJ. However, if you are written as this: cobject * & rpobj = myList.getat (POS);

Now, rpobj is a pointer to an object in a list, so when changing RPOBJ, it will also change the object address at the location POS in the list - in other words, replacing this object. 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-36974.html

New Post(0)