Difference between pointer and reference: pointer and reference

Source: Internet
Author: User
A reference is a pointer without a pointer syntax. Like a pointer, a reference provides indirect access to an object.
-- C ++ primer p29
Although a reference can also be used as a pointer, it is wrong to initialize a reference with an object address like a pointer.
For example:
Int I = 0;
Int & refi = I; // OK, refi points to an I reference
// Int & refi = & I; error, cannot be referenced with pointer Initialization

Once the reference has been defined, it cannot point to other objects (that is why it must be initialized ).
Reference all operations are actually applied to the objects it refers to, including the address fetch operation. For example:
Refi + = 2;
Equivalent to: I + = 2;

Int * j = & refi;
Equivalent to: int * j = & I;

-- C ++ primer p87

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 about **. 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.

1: Int & ABC (); what is the role of & in the function?
For example:
Int x = 0;
Int & A (Int & I)
I =-1;
Return X;
Main ()
Int J = 10;
A (j) = 100;
// At this time, j =-1, x = 100.
In this case, if a reference is returned, the content in the address of a variable is actually returned. It can be used as the left value to change the value of the referenced variable, after entering the function, J is a variable in the function I and outside the function, and the returned value and X are a variable. Therefore, after the value assignment, the values of J and X change, this method is often used in operator overload =, so it is necessary to master this knowledge.

2: What is the difference between clist <int, Int &> and clist <int>?
"Addition & mainly aims to improve the efficiency when a large object is taken as a parameter". For a large object, if a parameter is passed as a value, the entire object will be copied, it may take a lot of time, so clist allows it to customize the parameter types it accepts, which solves the above problem.
There is no difference in behavior between the two, but the difference is only the use of parameters.

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.