High quality c ++ programming guide Study Notes chapter 8th-thanks to Lin Rui

Source: Internet
Author: User

Chapter 8
Advanced features of C ++ Functions

Compared with C functions, C ++ adds four new mechanisms: overload, inline, const, and virtual. The overload and inline mechanisms can be used for both global functions and class member functions. The const and virtual mechanisms are only used for class member functions.

8. 1. Concept of function Overloading

Functions with the same name and scope can be overloaded. C/C ++ programs can ignore the return values of functions. Therefore, you cannot distinguish between overloaded functions based on the type of returned values. Because global functions and member functions have different scopes, they cannot constitute heavy loads.

Be careful that implicit type conversion leads to ambiguity in the overloaded functions. For example:

Void output (int x); // function declaration

Void output (float X); // function declaration

Because the number itself does not have a type, the type conversion is automatically performed when the number is used as a parameter. Output (0.5) will generate a compilation error because the compiler does not know whether to convert 0.5 to the Int or float type.

8. 2. Reload, overwrite, and hide member functions

8.2.1 overwrite: The Parent and Child classes must have virtual keywords for the parent class function. The function names and parameters are the same.

8.2.2 hiding rule: the function of the derived class shields the base class functions with the same name as the function of the derived class. Hide: The function names are the same between parent and child classes, and are hidden without virtual or parameters. Expand:

1) if the function of the derived class has the same name as the function of the base class, but the parameter is different. In this case, functions of the base class will be hidden no matter whether there is a virtual keyword.

2) If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual keyword.

What is the difference between overwriting and hiding? overwriting is the basis for implementing polymorphism. During overwriting, you can determine whether to call a base class function or a derived class function based on the object actually referred to by the pointer. The hidden function determines whether to call a base-class function or a derived-class function based on the pointer type.

How to get rid of hiding:

Class base

{

Public:

Void F (int x );

};

Class derived: public Base

{

Public:

Void F (char * Str );

};

Change derived:

Class derived: public Base

{

Public:

Void F (char * Str );

Void F (int x) {base: F (x );}

};

Default values of parameters 8 and 3

[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.

[Rule 8-3-2] If a function has multiple parameters, the parameter can only be left one by one from the back to the front.

Improper use of the parameter default value will lead to the ambiguity of the overload function.

8, 4 operator overload

The keyword operator is added with an operator to represent a function, which is called an operator overload. For example: complex operator + (const complex & A, const complex & B); differences between operators and common functions during calling: common functions, parameters appear in parentheses; for operators, parameters appear on the left and right sides of them. Some operators are not allowed to be reloaded.

8. function inline

The keyword inline must be put together with the function definition body to make the function called inline. The member functions defined in the class declaration are automatically called inline functions.

Use inline with caution: at the cost of code expansion (replication), inline only saves the overhead of function calls, thus improving the function execution efficiency. If the code in the function body is executed for a long time, the efficiency gains will be little compared to the overhead of function calling. On the other hand, the Code must be copied for each inline function call, which consumes more memory. Do not place the constructor and destructor definitions in the class declaration.

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.