C ++ advanced features

Source: Internet
Author: User

C ++ functions differ from C functions in four advanced features:

I. Four new mechanisms: overloaded, inline, const, and virtual.

Overload function conditions: 1. The function name is the same, 2. the same scope (that is, the member function of a class at the same time) 3. The number of parameters or types are different

Note: 1. The function only returns different types. This is not a function overload and an error is returned during compilation.

2. The same function name acts on different functions rather than function overloading. For example:

Void print (...); // Global functions
Class
{...
Void print (...); // Member functions
} Whether the parameters of the two print functions are different, if a member function of the class calls the global function print, to distinguish it from the member function print, the global function should be called ':: 'flag. For example
: Print (...); // Indicates that print is a global function rather than a member function.

1. How does the compiler distinguish between overloaded functions?

During compilation, the compiler makes an identifier based on the type and number of parameters of the overloaded function to indicate the differences.

2. Differences between overload and overwrite: override overwrites the parent class and Child class of the inheritance relationship of the application. The overwrite function must be marked by the virtual keyword, for example:

The base: F (INT) and base: F (float) functions are overloaded with each other, while base: G (void) is overwritten by derived: G (void.
# Include
Class base
{Public:
Void f (int x) {cout <"Base: f (int)" <x <endl ;}
Void f (float x) {cout <"Base: f (float)" <x <endl ;}
Virtual void g (void) {cout <"Base: g (void)" <endl ;}
}; Class Derived: public Base
{Public:
Virtual void g (void) {cout <"Derived: g (void)" <endl ;}
}; Void main (void)
{Derived d;
Base * pb = & d;
Pb-> f (42); // Base: f (int) 42
Pb-> f (3.14f); // Base: f (float) 3.14
Pb-> g (); // Derived: g (void)
}

Ii. Differences between confluence and macros:

Macros are replaced by pre-processors, while inline functions are implemented through compiler control. Both can improve program running efficiency

1. During program compilation, the compiler will replace the macro reference with the defined statement block to expand the referenced code:

Macro has two disadvantages: 1. Red cannot access private attributes

2. macros are prone to function ambiguity, for example:

For example, the following macro:

  # Define MAX (a, B) a> B? A: B

When we use macros like this, there is no problem: MAX (num1, num2); because the macro is expanded to num1> num2? Num1: num2 ;. However, if it is called in this way, MAX (17 + 32, 25 + 21); and an error occurs during compilation because the macro is expanded: 17 + 32> 25 + 21? 17 + 32: 25 + 21, Woh, what is this?

Therefore, when using a macro, you must add brackets to the parameters. The example above is changed to the following to solve the problem.

  # Define MAX (a), (B) (a)> (B )? (A) B)

Even so, there is still a Bug in this macro, because if I call MAX (I ++, j ++) like this; after this macro, I and j have been accumulated twice. This is definitely not what we want. Therefore, you should be careful when using macros, because the results of macro development are very unpredictable. In addition, although macro execution is very fast (because there is no function call overhead), the macro will make the source code crash and increase the size of the target file (for example, a 50-line macro, the program has
It is used in 1000 places, and it will be very difficult after macro expansion). On the contrary, it cannot make the program run faster (because the execution file grows and the system frequently changes pages during running ).

2. the inline function must be declared with the function body to be valid.

An Inline Tablefunction (int I) statement like this is ineffective. The Compiler just uses the function as a general statement of the number of functions. We must define the function body.

 

Inline tablefunction (int I) {return I * I };

 

This defines an inline function. We can call it like a common function. HoweverThe execution speed is indeed faster than that of common functions. We can also define external functions defined in the class as inline functions.For example:

 

Class tableclass {

PRIVATE:

Int I, J;

Public:

Int add () {return I + J ;};

Inline int Dec () {return I-j ;}

Int getnum ();

}

Inline int tableclass: getnum (){

Return I;

}

 

All the three functions stated above are inline functions. In C ++, the function of the function body is defined inside the class and is considered as an inline function by default. Whether or not you have the inline keyword. Inline functions in
The most widely used C ++ class should be used to define access functions. Generally, data members are defined as private or protected in the defined classes. In this way, the outside world cannot directly read and write data of our class members. To read and write private or protected members, you must use the member interface function.If we set If these read/write member functions are defined as inline functions, the efficiency will be improved.

 

Class sample {

Private:

Int nTest;

Public:

Int readtest () {return nTest ;}

Void settest (int I) {nTest = I ;}

}

 

Of course,Inline functions also have some limitations. That is, the Execution Code in the function cannot be too much.If the function body of the inline function is too large, the general compiler will discard the inline method and call the function in a normal way. In this way, the efficiency of inline functions is the same as that of normal functions.


Note: For more information about this blog, see other blog posts.

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.