Chapter 8 advanced features of C ++ functions (8.3 parameter default value 8.4 operator overload 8.5 function inline)

Source: Internet
Author: User

8.3 Default Value

Rules for using the default values of parameters:

[Rule 8-3-1] The default value of a parameter can only appear in the declaration of a function, but not in the definition body.

For example:

Void Foo (INT x = 0, int y = 0); // correct. The default value is displayed in the function declaration.

Void Foo (INT x = 0, int y = 0) // error. The default value appears in the definition body of the function.

{

Bytes

}

 

[Rule 8-3-2] If a function has multiple parameters, the parameter can only be taken from the back to the front by default. Otherwise, the function call statement is strange.

The correct example is as follows:

Void Foo (int x, int y = 0, int z = 0 );

The error example is as follows:

Void Foo (INT x = 0, int y, int z = 0 );

 

 

8.4 Operator Overloading

In C ++, operators can be added to the operator keyword to represent functions, which are called Operator overloading.

8.4.2 operators that cannot be overloaded

In the C ++ operator set, some operators cannot be overloaded. This restriction is designed for security reasons to prevent errors and confusion.

(1) The operator of the internal data type (such as int and float) of C ++ cannot be changed.

(2) You cannot reload '.' because '.' makes sense to any member in the class and has become a standard usage.

(3) do not reload symbols that are not present in the C ++ operator set, such as #, @, and $. There are two reasons: one is hard to understand, and the other is hard to determine the priority.

(4) When you reload existing operators, the priority rules cannot be changed; otherwise, confusion will occur.

 

 

8.5 function inline

8.5.1 replacing macro code with Inline

C ++ supports function inline to improve the function execution efficiency (speed ). In C Programs, macro code can be used to improve execution efficiency. Macro code itself is not a function, but it is used as a function. The pre-processor replaces the function call by copying macro code, saving the process of parameter pressure stack, generating call calls for the assembly language, returning parameters, and executing return, thus improving the speed. The biggest disadvantage of using macro code is that it is prone to errors, and The Preprocessor often produces unexpected marginal effects when copying macro code.

Inline is a keyword used for implementation, rather than a keyword used for declaration ".

The keyword inline must be put together with the function definition body to make the function inline. It does not work unless you put inline before the function declaration. The following function Foo cannot be an inline function:

// Inline is only put together with the function declaration

Void Foo (int x, int y)

{

}

The following function Foo becomes an inline function:

Void Foo (int x, int y );

Inline void Foo (int x, int y) // put inline together with the function definition body

{

}

The code of the inline function directly replaces the function call, saving the overhead of the function call. This process is significantly different from preprocessing because the pre-processor cannot perform type security checks or perform automatic type conversion. If the inline function is a member function, the object address (this) will be placed in a suitable place, which is not implemented by the Preprocessor.

 

 

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.