C ++ function parameter type-reference, pointer, Value

Source: Internet
Author: User
ArticleDirectory
    • Referenced Definition
    • Main referenced Functions

When writing a personal function, you will receiveC ++Limitation of a basic principle in: by default, a variable can only be passed to a function as a value. It means that only the value of the variable is passed to the function, and never the variable itself.

For example:

[CPP] View plain Copy Print ?
  1. VoidChangevar (IntMyvar,IntNewvalue)
  2. {
  3. Myvar = newvalue;
  4. }
  5. IntMain (){
  6. IntMynum = 20;
  7. Changevar (mynum, 90 );
  8. STD: cout <mynum <Endl;
  9. Return0;
  10. }

Void changevar (INT myvar, int newvalue) </P> <p >{</P> <p> myvar = newvalue; </P> <p >}</P> <p> int main () {</P> <p> int mynum = 20; </P> <p> changevar (mynum, 90); </P> <p> STD: cout <mynum <Endl; </P> <p> return 0; </P> <p >}</P> <p>

Result:20;

The reason for this is:Changevar() Function,ProgramJust putMain() FunctionMynumThe variable value is assignedChangevar() FunctionMyvarVariables. They are two different variables. In fact,Changevar() The function is completely unknown inMain() The function also hasMynumOr even its name.

Solution:

By using addresses and pointers

Bypass"Value Transfer"The first method to the problem is to pass the variable address to the function rather than its value.

[CPP] View plain Copy Print ?
    1. IntMain ()
    2. {
    3. IntMynum = 20;
    4. Changevar (& mynum, 90 );
    5. }

Int main () </P> <p >{</P> <p> int mynum = 20; </P> <p> changevar (& mynum, 90 ); </P> <p >}</P> <p>

Of course,Changevar() The function must also be modified accordingly.

[CPP] View plain Copy Print ?
    1. VoidChangevar (Int* Myvar,IntNewvalue ){
    2. * Myvar = newvalue;
    3. }

Void changevar (int * myvar, int newvalue) {</P> <p> * myvar = newvalue; </P> <p >}</P> <p>

Expected results can be obtained now.90.

This kind of exchange is in a lot of sortingAlgorithm.

Tip: Sometimes passing an address to a function is the only way for the function to receive a complex data type.

 

To"Reference Transfer"Method To pass parameters to the Function

Since using addresses like this is a good idea, isn't it better to improve this concept? If you know in advance that a function can only accept one address, can you write relatedCodeSo that you do not need to use special syntax when calling this function?

Introduce the reference transfer mode and enter parameters.

Referenced Definition

Int A = 100;

Int & B =;

B = 1000;

Test:(1) &And& B?(2) A =?

BNow yesAAn alias!A = 1000;

1) The reference must be initialized immediately upon declaration. null references are not allowed.

2) once the reference is initialized, other data cannot be referenced.

3) The referenced and referenced variables actually represent data in the same memory.

Main referenced Functions

Passing function parameters and return values. C ++There are three common methods

Value Transfer,Pointer transfer and reference Transfer

The nature of reference transfer is like pointer Transfer,Write form Image value transfer,

Reason:If you only need to borrow an alias,There is no need to use pointers.,.

Void changevar (Int & myvar, int newvalue );

The first input parameter of this function is not a pointer. It is an alias of the original variable that will be passed to this function. InChangevar() Any operation performed on this parameter variable in the function will be reflected inChangevar() The original variable outside the function. This meansChangevar() Functions are the same as the original ones.

Void changevar (INT myvar, int newvalue ){

Myvar = newvalue;

}

This makes it easier to call this function.----Only one variable name is required;

Int main(){

Int mynum = 20;

Changevar (mynum, 90 );

}

This is easier than passing parameters.

To"Reference transfer"Method to pass the parameter value to a functionC ++To make the function calling syntax simpler and clearer.

Tip: 1. When defining a function, you can also make it"Reference Transfer"Instead"Value Transfer"Method return:Int & myfuntion();

2. Besides changing the values of related variables,"Reference Transfer"Another advantage of the method is that its overhead is relatively small: Because you do not need to create a temporary variable in the function to accommodate those values, the memory usage of the program is certainly smaller.

3. If you want to obtain"Reference Transfer"But you do not want to change the value of a variable, you can define the corresponding input parameter as a constant:

Void myfunc (const Int & mynum );

The function defined in this way can pass the specific parameters directly to it:Myfunc (7 );

 

 

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.