Void func1 (myclass * & pbuildingelement;
This issue is frequently asked in forums. This article attempts to explain this problem through some practical pointer usage experience.
Taking a closer look at this declaration method is a bit confusing. In a sense, "*" and "&" mean two opposite things. What is the significance of putting them together ?. To understand this practice of pointers, let's first review the ubiquitous pointer concept in C/C ++ programming. We all know the meaning of myclass *: pointer to an object. The object type is myclass. Void func1 (myclass * pmyclass );
// Example: myclass * P = new myclass;
Func1 (P );
The above sectionCodeWho must have used this method? Create a myclass object and pass it into the func1 function. Now assume that this function needs to modify pmyclass: void func1 (myclass * pmyclass)
{
Dosomething (pmyclass );
Pmyclass = // pointer of other objects
}
The second statement modifies only the pmyclass value in the function process. The P value of the caller's variable is not modified. If P points to an object at address 0x008a00, it still points to this specific object when func1 returns. (Unless there is a bug in func1 that will disrupt the heap, this is entirely possible .)
Now suppose you want to modify the value of P in func1. This is your right. The caller passes in a pointer and the function assigns a value to the pointer. In the past, we generally used to transmit double pointers, that is, pointer, for example, cmyclass **. Myclass * P = NULL;
Func1 (& P );
Void func1 (myclass ** pmyclass );
{
* Pmyclass = new myclass;
......
}
After func1 is called, P points to a new object. In COM programming, you may encounter this usage 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 of the someinterface type, so & P is the pointer. When QueryInterface returns, if the call is successful, the variable P contains a pointer to the new interface.
If you understand pointers, you must understand pointer references because they are exactly the same thing. If you declare a function as follows:
Void func1 (myclass * & pmyclass );
{
Pmyclass = new myclass;
......
}
In fact, it is similar to the pointer example mentioned above, but the syntax is different. When transferring, you do not need to pass p's address & P, but directly transfer P itself:
Myclass * P = NULL;
Func1 (P );
After the call, P points to a new object. Generally, the reference principle is more or less like a pointer. In terms of syntax, it is a common variable. So as long as you encounter * &, you should think of it **. That is to say, this function modifies or may modify the caller's pointer. The caller passes the pointer like a common variable without using the address operator &.
I will say that this method is rarely used. MFC uses it in its collection class-for example, coblist, which is a cobjects pointer list. Class coblist: Public cobject {
......
// Obtain/modify the element at the specified position
Cobject * & getat (position );
Cobject * getat (position) const;
};
There are two getat functions, which are used to obtain elements at a given position. What is the difference?
The difference is that one allows you to modify objects in the list, and the other does not. So if you write it as follows: cobject * pobj = mylist. getat (POS );
Pobj is the pointer to an object in the list. If you change the value of pobj: pobj = psomeotherobj;
This does not change the object address at the position POs, but only the variable pobj. However, if you write the following code: cobject * & rpobj = mylist. getat (POS );
Now, rpobj refers to the pointer of an object in the list, so when rpobj is changed, the object address at the position POs in the list will also be changed-in other words, this object is replaced. This is why coblist has two getat functions. One can modify the pointer value, and the other cannot. Note that I am talking about pointers, not objects. Both functions can modify objects, but only * & versions can replace objects.
References in C/C ++ are important and efficient processing methods. Therefore, if you want to become a C/C ++ expert, you will not be able to thoroughly understand and use the referenced concepts.