Basic concepts of C ++ Functions

Source: Internet
Author: User

The C ++ programming language brings us great benefits. However, even the features we often use have a lot of profound content that deserves further discussion. Here we will give you a detailed introduction to the basic concepts of C ++ functions for your convenience.

C ++ function imitation is often used in the template library, such as STL. So what is a function imitation?

As the name implies: a function is something that can work like a function. Forgive me for using something as a pronoun. I will explain it slowly below.

 
 
  1. void dosome( int i )  

This dosome is a function. We can use it like this: dosome (5 );

So, is there anything that can work like this?

Answer 1: objects that overload the () operator, such:

 
 
  1. struct DoSome   
  2. {   
  3. void operator()( int i );   
  4. }   
  5. DoSome dosome;  

Here the class (for C ++, struct and class are the same) overload the () operator, so its instance dosome can use dosome (5) like this ); it is exactly the same as the above function call, isn't it? So dosome is a C ++ function.

In fact, there is still answer 2:

The object to which the function pointer points.

 
 
  1. Typedef void (* DoSomePtr) (int );
  2. Typedef void (DoSome) (int );
  3. DoSomePtr * ptr = & func;
  4. DoSome & dosome = * ptr;
  5. Dosome (5); // This is exactly the same as the function call.

Of course, Answer 3: The member function pointed to by the member function pointer is the expected answer.

  • Detailed explanation of C ++ timing operations
  • Introduction to C ++ pre-processing commands
  • Introduction to logical partition of C ++ memory
  • How to disable the C ++ heap object
  • How to disable C ++ stack objects

C ++ Functions

Objects, function pointers, and so on can be passed as parameters or saved as variables. Therefore, we can pass a function imitation to a function, which calls a similar callback as needed ).

This technique is widely used in the STL template library to achieve "flexibility" of the library ". For example, for_each has the following source code:

 
 
  1. template < typename Iterator, typename Functor >   
  2. void for_each( Iterator begin, Iterator end, Fucntor func )   
  3. {   
  4. for( ; begin!=end; begin++ )   
  5. func( *begin );   

This for loop traverses every element in the container and calls the func function for each element, so that the idea of doing the same thing for each element is realized. In particular, if the imitation function is an object, this object can have member variables, which gives the C ++ imitation function a "state" and achieves higher flexibility.

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.