C ++ function call method (real parameter)

Source: Internet
Author: User

Value transfer method:

1. Common call. The real parameters involved in the form are of the same type.

Fun (int I )()

Fun (student stud)

()

Main ()

{

Fun (1 );

Student S1;

Fun1 (S1 );

}

You can modify the value of a parameter in a function, but cannot change the value of the corresponding real parameter (the value change of the parameter cannot be transferred to the real parameter: during function calling, a new object is created, which is a copy of the real parameter object. The real participating parameters occupy different storage spaces. no matter whether the parameters are modified or not, the value of the real parameter is not changed. In this form, the combination of real and false will generate a copy of the real parameters. When the transferred object is large, both the time overhead and the space overhead are large.

2. Use pointers as function parameters

Fun (int * I)

{}

Fun (student * stud)

{}

Main ()

{

Int I = 1;

Fun (& I );

Student S1;

Fun1 (& S1 );

}

The essence is also the value transfer method, but the passed value is an address. The real parameter passes the address to the form parameter. After the actual parameter is combined with the actual parameter, the real participation parameter points to the same address. It operates on the same object. When combined with real-world, a copy of the real parameters will also be generated, allocating memory for the shape parameters, used to store the pointer value (that is, the address)

2. Reference Transfer Method

1. Use reference as function parameter

Fun (Int & I ){}

Fun (student & stud ){}

Main ()

{

Int I = 1;

Fun (I );

Student S1;

Fun1 (S1 );

}

Instead of allocating storage space for parameters (often called a copy of a created real parameter), it passes the address of the real parameter to the parameter (reference name), and the reference name also points to the real Parameter Variable

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.