C ++: new features of c ++ Functions

Source: Internet
Author: User

1. inline functions: These functions exchange time at the cost of space (memory space (ProgramExecution efficiency). recursion is not allowed. It can be used for value transfer or address transfer, while macros cannot

Value transfer. Inline functions are generally used in the following situations: the program is called multiple times, and the functionCodeThe execution time is short.

2. Reference (variable): Mainly used as the form parameter of a function.

3. Differences between reference and pointer:

(1) The reference must be initialized at the time of its Declaration, that is, it must be initialized at the time of creation. Unlike the pointer, it can be declared first and then initialized.

# Include <iostream>

Using namespace STD;

Int main ()

{

Int rat = 100;

Int & Shu = rat;

Cout <"rat =" <rat;

Cout <"," <"Shu =" <Shu <Endl;

Cout <"rat address:" <& rat;

Cout <"," <"Shu address:" <& Shu <Endl;

Int cat = 50;

Shu = cat;

Cout <"cat =" <cat;

Cout <"," <"rat =" <cat;

Cout <"," <"Shu =" <Shu <Endl;

Cout <"cat address:" <& CAT;

Cout <"," <"Shu address:" <& Shu <Endl;

Cin. Get ();

Return 0;

}

The result of running the above program is as follows:

 

The above results show that the value of rat is also changed to 50. Meanwhile, the addresses of rat and Shu are the same, and the addresses are different from those of cat. Because Shu is the alias of rat, the SHU = cat statement is equivalent to the following sentence: Rat = cat; it also means that the cat variable value is assigned to the rat variable. In the above example, you can set the reference through the initialization declaration, but you cannot set the reference through the value assignment. Otherwise

Unexpected and serious consequences may occur.

4. A reference function is used as a form parameter, but note: when using a reference as a parameter (form parameter), it is likely to get used to the value of the function called, that is, the value of the real parameter.

Temporary variables, reference parameters, and const:

If the referenced parameters do not match, C ++ generates temporary variables. Currently, C ++ allows this only when the parameter is referenced by const.

If the referenced parameter is const, temporary variables are generated in the following two cases:

A. The real parameter type is incorrect, but it is not the left value.

B. The real parameter type is incorrect, but can be converted to the correct type.

The left value parameter is a reference object, for example, a variable, an array element, a structure member, or a referenced or unbound pointer is a left value.

Non-left values include literal constants and expressions that contain multiple numbers.

Example:

Double refcube (const double & RA)

{

Return a *;

}

Double side = 3.0;

Double * Pd = & side;

Double & RD = side;

Long Edge = 5l;

Double Lens [4] = {1.0, 2.0, 3.0, 4.0 };

Double C1 = refcube (side); // Ra is side;

Double C2 = refcube (Lens [2]); // Ra is Lens [2]

Double C3 = refcube (RD); // Ra is RD is side

Double C4 = refcube (* PD); // Ra is * PD is side

Double C5 = refcube (edge); // Ra is temporary variable

Double C6 = refcube (7.0); // Ra is temporary variable

Double C7 = refcube (side + 10.0); // Ra is tempoary variable

The side, Lens [2], RD, and * PD parameters are named double data objects, so you can create references for them without the need for temporary variables. However, edge is a variable and type.

But it is incorrect. The double reference cannot point to long. In addition, the parameters 7.0 and side + 10.0 are of the correct type but have no name. In this case, the compiler will generate a temporary anonymous variable,

And let Ra point to it. These temporary variables only exist during the function call and can be deleted by the compiler.

In no special circumstances, the reference of the form parameter is set to const. cosnt should be used as much as possible for the following reasons:

(1) Using const can avoid accidental data modification programming errors.

(2) Use const to enable the function to process const and non-const real parameters; otherwise, only non-const data can be accepted.

(3) Use cosnt reference to enable the function to correctly generate and use temporary variables

5. The second role of reference: used for structure.

# Include <iostream>

Using namespace STD;

Struct sysop

{

Char name [26];

Char quote [64];

Int used;

};

Const sysop & use (sysop & sysopref)

{

Cout <sysopref. Name <"says:" <Endl;

Cout <sysopref. Quote <Endl;

Sysopref. Used ++;

Return sysopref;

}

Int main ()

{

Sysop logoff =

{

"Caizhiming ",

"Bao Jianfeng from honed out, plum blossom from bitter cold ",

0

};

Use (logoff );

Cout <"Logoff:" <czm. Used <"use (s)" <Endl;

Sysop copycat;

Copycat = use (logoff );

Cout <"lorule:" <lorule. Used <"use (s)" <Endl;

Cout <"copycat:" <copycat. Used <"use (s)" <Endl;

Cout <"use (logoff):" <use (logoff). Used <"use (s)" <Endl;

Cin. Get ();

Return 0;

}

In the code above, the reference is used as the return value. In general, the Return Mechanism copies the return value to the temporary storage region, and then the caller accesses the region. Then, the returned reference means that the caller will directly access the return value without copying it. Generally, the reference will point to the reference of the passing function. Therefore, calling a function actually directly accesses a variable of its own. For example, in the above example, sysopref is a reference to logoff, so the returned value is the original logoff variable in the main function.

When returning a reference, note: avoid returning memory unit references that no longer exist when the function is terminated, and avoid returning pointers pointing to temporary variables when using pointers!

6. The third role of reference: used for class objects

When passing class objects to functions, C ++ usually uses references. For example, you can reference the object of the class string, ostream, istream, ofstream, and ifstream as parameters. If a class is derived from another class, the reference of the base class can point to the object of the derived class.

7. When to use references, when to use pointers, and when to pass by value, the following principles are generally followed:

(1) functions that use passed values without modification:

A. if the data object is small, such as the built-in data type or small structure, it is passed by value.

B. If the data object is an array, use a pointer because it is the only choice and declares the pointer as a pointer to the const.

C. If the data object is of a large structure, use the const pointer or const reference to improve program efficiency. This saves the space and time for the replication structure.

D. If the data object is a class object, use const reference. Reference is often required for the semantics of class design, which is the main reason for C ++'s new reference. Therefore, the standard way to pass class object parameters is

Pass by reference.

(2) For the function that modifies the data in the called function:

A. if the data object is of the built-in data type, use a pointer. If you see code like Fixit (& X) (where X is of the int type), change the function to change X.

B. If the data object is an array, use a pointer.

C. If the data object is a structure, use a reference or pointer.

D. If the data object is a class object, reference is used.

The introduction of references only applies to structures and class objects.

8. Function overloading: the Key to function Overloading is the parameter list, also known as the function feature mark. Function overloading must be based on feature labels, and the return types can be different. Function names are the same, but feature labels (parameter lists) are different, namely, function polymorphism (or function overloading ). The function template automatically completes the process of reloading the function.

9. function template: The template <class T> is not an independent statement. It must be integrated with the method definition declaration or class declaration, although sometimes written in two lines.

Sample Code:

# Include <iostream>

Using namespace STD;

Template <class T>

Void swap (T & A, T & B );

Template <class T>

Void swap (T & A, T & B)

{

T temp;

Temp =;

A = B;

B = temp;

}

Int main ()

{

Int I = 10;

Int J = 20;

Cout <I <"," <j <Endl;

Swap (I, j );

Cout <I <"," <j <Endl;

Double M = 12.34;

Double N = 20.57;

Cout <m <"," <n <Endl;

Swap (m, n );

Cout <m <"," <n <Endl;

Cin. Get ();

Return 0;

}

10. default parameters:

If the N parameter defaults, the n + 1 n + 2 ...... All must be the default parameters, so the default parameters should be placed in the "back" of the parameter list. That is to say, to set a parameter to the default value, you must provide the default value for all subsequent parameters.

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.