Value passing, reference passing, pointer passing

Source: Internet
Author: User

Original address:
Http://www.cnblogs.com/yanlingyin/archive/2011/12/07/2278961.html
Value Delivery:

A formal parameter is a copy of an argument, and changing the value of the parameter does not affect the value of the external argument. From the point of view of the invoked function, the value pass is a one-way (argument-> parameter), the value of the argument can only be passed in and cannot be transmitted. When the function needs to modify the parameters, and do not want this change to affect the caller, the use of value delivery.

Pointer Pass:

A parameter is a pointer to an argument's address, which is equivalent to an operation on the argument itself when the parameter is pointed to.

Reference delivery:

A formal parameter is equivalent to an "alias" of an argument. The operation of formal parameters is actually the operation of the actual parameters, in the process of reference transfer, the form parameter of the modulated function, although also as a local variable in the stack to open up the memory space, but at this time is stored by the main function to put in the argument variable address. Any operation of the called function on the formal parameter is treated as indirection, that is, the argument variable in the keynote function is accessed through the address stored in the stack. Because of this, any manipulation of the function on the formal parameter affects the argument variables in the keynote function.

Code test:

#include <iostream>
using namespace std;
Value delivery
void change1 (int n)
{
    cout << "value pass--function operation Address" << &n << Endl;
    n++;
}      
Reference delivery
void change2 (int &n)
{
    cout << "reference pass--function operation Address" << &n << Endl;
    n++;
}
Pointer pass
void change3 (int *n)
{
    cout << "pointer pass-function operation Address" << N << Endl;
    *n = *n + 1;
}
int main ()
{
    int n=10;
    cout << "The address of the argument" << &n << Endl;
    Change1 (n);
    cout << "value passed after n=" << n << Endl;
    Change2 (n);
    cout << "n= after Reference" << n << Endl;
    Change3 (&n);
    cout << "Pointer pass after n=" << n << Endl;
    return true;
}

Run Result:

References and pointers:
★ The same point:
Are the concept of the address;
The pointer points to a piece of memory whose contents are the address of the memory, and the reference is an alias to a block of memory.

★ Different points:
The pointer is an entity and the reference is only an individual name;
References can only be initialized once at definition, then immutable, pointer variable, reference "one-woman", and pointers may be "inconstant";
The reference does not have a const, the pointer has a const,const pointer is immutable; (specifically, there is no int& const a form, and const int& A is, the former guidelines used in itself that alias can not change, of course, so do not need this form, The latter refers to the value of the reference can not be changed)
The reference cannot be null, the pointer can be empty;
The "sizeof reference" gets the size of the variable (object) to which it is pointed, and the "sizeof pointer" gets the size of the pointer itself;
The value of the pointer and the reference of the self increment (+ +) operation is different;
is type-safe, and pointers are not (references have more type checks than pointers) about references:

References introduce a synonym for an object. The presentation of the definition reference is similar to the definition pointer, except that the * is replaced with &.
A reference must be initialized immediately when it is defined, because it must be a synonym for something. You can't just define a reference before
Initialize it. For example, the following statement is illegal:
Point &pt3;
PT3=PT1;

So since the reference is just a synonym for something, what is the use of it?
The two main uses of the reference are discussed below: As a function argument and a left value from a function.

1. Passing Variable parameters
In traditional C, when a function is invoked, the parameter is passed by value, which means that the parameter of the function does not have the ability to return a value.
So in traditional C, if the function's parameter has the ability of returning a value, it is often realized by the pointer. For example, to achieve
The C program for exchanging two integer variable values is as follows:
void Swapint (int *a,int *b)
{
int temp;
Temp=*a;
A=*b;
*b=temp;
}

After using the referral mechanism, the C + + version of the above program is:
void Swapint (int &a,int &b)
{
int temp;
Temp=a;
A=b;
B=temp;
}
The C + + method that calls the function is: Swapint (x,y); C + + automatically passes the X,y address as a parameter to the Swapint function.

2. Passing large objects to functions
When a large object is passed to a function, the use of reference parameters can increase the efficiency of parameter passing because the reference does not produce an object's
Copy, that is, when the parameter is passed, the object does not need to be copied. The following example defines a class with a finite set of integers:
Const MAXCARD=100;
Class Set
{
int Elems[maxcard]; Sets, and Maxcard represents the maximum number of elements in the collection.
int card; The number of elements in the collection.
Public
Set () {card=0;}//constructor
Friend set operator * (set, set); Overloaded operation symbol *, used to compute the intersection of a set with an object as a pass-value parameter
Friend set operator * (Set &, set &) overloaded operation symbol *, used to compute the intersection of the collection with the reference of the object as the parameter of the pass value
...
}
First consider the implementation of set intersection
Set operator * (set Set1,set Set2)
{
Set Res;
for (int i=0;i

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.