C ++ pointer usage method

zhaozj2021-02-16  84

?

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? * & pbuildingeltingelement?);??

?

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 to use, create a MyClass object, and then pass it into the FUNC1 function. Now suppose this function wants to modify PMYCLASS:? Void? FUNC1 (MyClass? * PMYCLASS)?

{?

DOSMETHING (PMYCLASS);?

PMYCLASS? =? //? Other objects of 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, there is a lot of possibilities.)?

?

Now suppose you want to modify the value of 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 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 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 are like this, the function is declared:?

?

?

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, but directly pass the 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 the element 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 write to the following:? COBJECT *? POBJ? =? MyList.Getat (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 writing:? 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-13475.html

New Post(0)