C ++ pointer usage

Source: Internet
Author: User

In the following function declaration, why do I need to use both * and? And on what occasions does this statement work?
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 code must have been used to 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 position );
Cobject * GetAt (POSITION 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.