Value passing, reference passing, and pointer passing in C + +

Source: Internet
Author: User
Tags expression reference

C + + value delivery, reference passing, pointer passing these concepts have been plagued by C + + programmers, I have never been able to tell the relationship between them through a simple example, or simply to say, find some relevant information, and personal feelings, a brief introduction.

There are three different ways to pass parameters in C + +: The value of the parameter (called the value transfer, referred to as the value), the address of the parameter (called address delivery, referred to as the call), and reference transfer (referred to as a reference), the corresponding function is the value call, address calls and reference calls

When a function is defined, the parameter in the parameter table is called the formal parameter, and the parameter in the parameter table is called the actual parameter. referred to as the actual parameter, the data transfer between the argument and the formal parameter is called the combination of form and reality, usually the C + + language is a transfer value call, the value call is one-way, that is, the value of the parameter can only be passed to the formal parameters by the argument, Instead of being passed to a value parameter by a formal parameter, that is, from the point of view of the called function, the value of the parameter can only be passed in and not outgoing. An argument can be a specific value, a variable that is already a value, and an expression that can be evaluated, because the values and variables are one of the expressions, so the argument is essentially an expression when the value is called. When the function is called, the system first to the argument expression of the ball value, and then pass the value to the formal parameter, replaced a thought, when the value call, the formal parameter is actually a copy of the argument, so the transfer does not change the value of the external argument argument,

Suppose declaring a variable int n=1; Then he stored in memory, we can understand, first: in memory 1 refers to the value of the variable n, the address that stores this value we call him 1 of the memory address, that is, the pointer address, N is his name, or we call him 1 of the name, In addition, we can give him a different name, called nickname, also known as Alias, because the name and parting are pointing to the same value, so change the name of the value is the name of the value of a nickname, nickname is a reference to name, clear up these meanings, we will give an example.

int m;

int &n=m;

N is a reference to M reference,m is a referenced object referent. n equals the alias of M,

Some of the rules cited are as follows:

(1) When a reference is created, it must be initialized (the pointer can be initialized at any time).

(2) cannot have a null reference, the reference must be associated with a legitimate storage unit (the pointer can be null).

(3) Once the reference is initialized, the referenced relationship cannot be changed (the pointer can change the object at any time).

The primary function of a reference is to pass the function's arguments and return values.

In the C + + language, the parameters and return values of functions are passed in three different ways: value passing, pointer passing, and reference passing.

The following is a sample program for value delivery.

Since x in the FUNC1 function body is a copy of the external variable n, changing the value of x does not affect n, so the value of n is still 0.

#include <iostream>

using namespace Std;

func (int x);

void Main () {

int n=0;

Func (n);

cout<<n<<endl;

}

func (int x) {

x = x + 10;

}

Related Article

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.