Pointer and pointer reference

Source: Internet
Author: User
Research on C ++ -- pointer and reference

There are two transfer methods for the number of functions or procedures in VB: value transfer and reference transfer. Use keywordbyval and keywordbyref respectively. If the number of arguments is passed by reference, the external variable value can be changed by reference to the number of arguments in a function or process. In C, if you want to change the value of an external variable in the current function, you should pass the pointer of this variable. If you want to use the pointer to ask a variable, you must use the pointer operator "*". In this way, it will look awkward in the source code:

Void function (int * pval)
{
* Pval = 100;
// Pval = 100; The type conversion error is not considered here.
// This code can only change the address of the temporary pointer variable in the stack, but cannot change the value of the pointer pointing to the object.
}

Int main ()
{
Int x = 200;
Function (& X );
Return 0;
}

In order to transparently use pointers to issue variables, the concept of "Reference" is introduced in C ++:


Void function (Int & refval)
{
Refval = 100;
}

Int main ()
{
Int x = 200;
Function (X );
// Of course, for example, the following call can also be performed. But in this way, the original meaning of introducing "Reference" is lost.
Int & refx = X;
Function (refx );
Return 0;
}

In this way, you only need to modify the function declaration to achieve the consistency between the pointer and the general issue at the source code level. We can think of "Reference" as a pointer that can be used to ask variables without the "*" operator. Pseudocode in the C language of the above Code:

Void function (int * refal)
{
* Refval = 100;
}

Int main ()
{
Int x = 200;
Int * refx = & X;
Function (& X );
Function (refx );
Return 0;
}

According to the declaration of the function, the C ++ compiler automatically converts the binary code in the form of "function (& X);" when encountering a "function (x);" statement.

Let's see the difference between "int ** pp" and "int * & RP. The former is a pointer to the pointer, and the latter is a reference to the pointer. If this is not clear, it will be clear after the change:

Typedef int * lpint;
Lpint * PP;
Lpint & RP;

The binary code of the following two functions is consistent:

Void function1 (INT ** P)
{
* P = 100;
* P = NULL;
}

Void function2 (int * & ref)
{
* Ref = 100;
Ref = NULL;
}

When function1 or function2 is called, the binary code compiled by the compiler will pass a dual pointer.

"Reference", however, does not differ in nature from pointers to provide a convenient way for heavy-duty operators.

Why should I use the * and & symbols at the same time when referencing pointers in C ++ in the following function declaration? And on what occasions does this statement work?

Void func1 (myclass * & pbuildingelement );

This type of question is frequently asked in forums. This article attempts to explain this problem through some practical pointer usage experience.

Taking a closer look at such a statement is a bit confusing. In a sense, "*" and "&" mean two opposite things. What is the significance of putting them together ?. To understand pointer practices, 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 );

// For example, myclass * P = new myclass;

Func1 (P );

Whoever has used this processing method in the above Code, creates a myclass object, and passes it into the func1 function. Now if this function needs to change pmyclass: void func1 (myclass * pmyclass)

{
Dosomething (pmyclass );
Pmyclass = // pointer of other objects
}

The second statement only modifies the pmyclass value in the function process. The P value of the caller's variable is not changed. Suppose P points to an object at the address 0x008a00. When func1 returns, it still points to this specific object. (Unless there is a bug in func1 that will mess up the heap, this is entirely possible .)

Now, if you want to change 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 can use this method 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, assuming that 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 all the same thing. Assume that you declare a function as follows:

Void func1 (myclass * & pmyclass );
{
Pmyclass = new myclass;
......
}

In fact, it is just a different syntax than the pointer example mentioned above. 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 you just need to meet * &, you should think **. That is to say, this function may change the caller's pointer. The caller will pass 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 object allows you to change the list, and the other one does not work. So suppose you write it as follows: cobject * pobj = mylist. getat (POS );

Then pobj is the pointer to an object in the list. Assume that the value of pobj is changed: pobj = psomeotherobj;

This does not change the object address at the position POs, but changes the variable pobj. However, assume that 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. A value that can change the pointer, and another value cannot. Note that I am talking about pointers, not objects. Both functions can modify objects, but only the * & version number can replace objects.

References in C/C ++ are very important and efficient processing methods at the same time. Therefore, if you want to become a C/C ++ expert, you will not be able to thoroughly understand and use the referenced concepts.

Pointer and pointer reference

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.