What functions, function templates, function objects, function object templates, and callback mechanisms in C ++ have to say

Source: Internet
Author: User

Why c ++ is awesome? Because only what you can't think of is impossible to do without C ++...

 

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------

 

I have learned the generic templates in STL in C ++ before. This kind of versatility makes people who use it so angry and excited ~~!

 

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------

 

Next, we will learn about functions, function templates, function objects, and function Object Templates. These things may be more useful in C ++ programming and project development...

 

The callback mechanism implements inter-class message transmission in C ++. What happens when both of them encounter together ~~~

 

First look at a piece of code:

 

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------

 

If you directly paste the code here, the layout format here is not very good, and you will post it:

 

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------

 

1> A is a common function that compares implementation parameters with 7,

 

2> function B implements a simple callback function.

 

Note: I was just testing and didn't think too much about it. Here, A, C, and D are of the type and are used as Callback functions (function passing parameters ),,,

The B function is used to implement callback, that is, to call a, c, d... sorry for the inconvenience caused to the reader ....

 

Explanation: Why is a template used here .. very simple. Think about the features of the template. Yes, it's good, because B is a common function (the function header is very common, but the function body is not universal. haha, this reader can study how to use this function on his own), (Don't underestimate this function (its function body has only one sentence ))

 

3> C is a class used as a function object. It is implemented and the parameter is compared with any given value (this is the essence of the function object, it can be regarded as a set of Class A functions or an abstract function. You can understand it as needed)

 

4> Class D is a template function object that compares any type with any given type.

Note 1: Of course, its implementation is not universal. (It can be easily seen that only the type of> number can be compared here)

NOTE 2: Of course, it can be extended. For example, only comparison like> is implemented here .. you can add any operation. Similarly, only return bool is implemented here, or you can add the other returned results you want. (It is not difficult to implement the reload .)

 

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------

 

Finally, I wish you progress every day ......

Make progress together !!~~~~

 

Reprinted please indicate the source: http://blog.csdn.net/jing51536

Thank you !!~~

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.