C ++ primer study Note 2

Source: Internet
Author: User
Document directory
  • 7.1.1 function return type
  • 7.2.1 non-reference parameters
  • 7.2.2 reference parameters
  • 7.2.4 array parameters

 

There was moderate rain on Tuesday

 

Keywords: form parameter, real parameter, Function

6.14 use a pre-processor for debugging

 

1 The Four constants defined by the Preprocessor are often used during debugging:

_ File name

_ Line _ current row number

_ Time _ file Compilation Time

_ Date _ Date on which the file is compiled

 

2 assert preprocessing macro, which is defined in the cassert. h header file. This header file must be included during use.

 

7.1 Function Definition 7.1.1 function return type

1. function return types can be built-in type, class type, composite type (such as pointer type), and void type.

2. A function cannot return another function or a built-in array type. It can return a pointer to a function or a pointer to a data element.

7.2 passing parameters 7.2.1 non-reference parameters

 

The initialization of a parameter is the same as that of a variable. If the parameter has a non-reference type, the value of the real parameter is copied. If the parameter is of the reference type, it is only the alias of the real parameter.

2. Common Non-reference parameters are initialized by copying the corresponding real parameters. Because the parameter is initialized using a copy of the real parameter, the function does not access the passed real parameter itself and does not modify the value of the real parameter.

3. A non-reference real parameter only indicates a local copy of the corresponding real parameter. Modifications to such parameters only change the value of a local copy. Once the function execution ends, the values of these local variables will no longer exist.

4. Pointer Reference

In a function, pointer arguments do not change the object to which the pointer argument points before passing it, but they can change the value of the object to which it points.

Int B = 20;

Void reset (int * IP)

{

* IP = 0;

Int A = 20;

IP = & B;

}

Void main (void)

{

Int I = 42;

Int * P = & I;

 

Cout <"I =" <I <Endl;

Cout <"* P =" <* P <Endl;

Cout <"P =" <p <Endl;

Reset (P );

Cout <"===========" <Endl;

Cout <"I =" <I <Endl;

Cout <"* P =" <* P <Endl;

Cout <"P =" <p <Endl;

}

 

Actual output:

I = 42

* P = 42

P = 0012ff60

==============

I = 0

* P = 0

P = 0012ff60

 

5 const Parameters

If a function uses a non-referenced non-const parameter, you can pass the const and non-const real parameters to the function. Here, because the initial value is copied during initialization, it is only a copy.

If it is a const-type parameter. Because real parameters are still transmitted in the form of replicas, you can still use the const object or non-const object to pass real parameters. However, this local copy of real parameters cannot be changed within the function at this time, this parameter is a const object within the function.

 

6. Limitations of copying real parameters

It is not suitable for copying real parameters:

1) when you need to modify the value of the real parameter in the function.

2) When a large object needs to be passed as a real parameter. For practical applications, a large amount of time and storage space is required.

3) objects cannot be copied.

 

7.2.2 reference parameters

1 const reference to avoid copying and modifying real parameters.

2. Non-const reference parameters can only be associated with non-const objects of the same type.

3. You should define the parameters that do not need to be modified as const references.

4 int * & V1 from right to left: V1 is a reference and associated with a pointer to an int type object. When using this form as a form parameter, what is passed to the function is just a pointer alias.

5. Generally, containers are not used as function parameters.

 

7.2.4 array parameters

1. the array parameters of the function can be written as arrays, but they are of the INI * type.

2. It is written as an array, and the length of the form parameter may cause misunderstanding. During program compilation, the length of the array parameter in the array parameter is not checked. If you want to pass the length, you can only display the pointer at the next position of the first and last elements that are passed to the function length or to the Function Array ..

3. Pass the array by referencing.

Format: void printvalues (INT (& ARR) [10]);

In this case, the compiler checks whether the size of the array real parameter matches the size of the form parameter.

 

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.