Beginners must read C/C ++ pointer applications

Source: Internet
Author: User

C ++The member pointer is the most complex syntax structure. However, it is widely used in event-driven and multi-threaded applications to call-back functions. In multi-threaded applications, each thread calls this function by pointing to a member function pointer. In such an application, it is very difficult to program without Member pointers.

When you encounter this syntax, it may stop you. However, you will find that the complex syntax can be simplified after the appropriate type definition is used. This article shows you how to declare member function pointers, assign values, and call functions.

Member function pointer Declaration

A member function pointer includes the return type of the member function, followed by: operator class name, pointer name, and function parameters. At first glance, the syntax is a bit complex. It can be understood as a pointer to the original function. The format is: function return type, class name,: operator, pointer asterisk, pointer name, and function parameter.

A pointer to an external function is declared:

 
 
  1. void (*pf)(char *, const char *);  
  2. void strcpy(char * dest, const char * source);  
  3. pf=strcpy; 

A pointer to A Class A member function is declared:

 
 
  1. void (A::*pmf)(char *, const char *); 

The Declaration explains that pmf is A pointer to A member function and returns A non-type value. The function has two parameters whose types are char * and const char *. In addition to adding A: Before the asterisk, the method is the same as that used to declare the external function pointer.

Assignment

You can assign a value to a member pointer by using the pointer symbol. As follows:

 
 
  1. class A  
  2. {  
  3. public:  
  4.  void strcpy(char *, const char *);  
  5.  void strcat(char *, const char *);  
  6. };  
  7. pmf = &A::strcpy; 

Some old compilers can assign an ampersand (&) without an ampersand (&), but the standard C ++ enforces the Ampersand.

Use Type Definition

You can use the type definition to hide complex member pointer syntax. For example, the following statement defines that PMA is A pointer to A member function, and the function returns A non-type value. The function parameter type is char * and const char *:

 
 
  1. Typedef void (A: * PMA) (char *, const char *);
  2. PMA pmf = & A: strcat; // pmf is A variable of the PMF type (Class A member pointer ).

The following shows that the use of the type definition is particularly helpful for declaring a member pointer array.

Call a member function using a member pointer

You can call a member function of an object through a member pointer without having to know the function name. For example, the dispatcher function has a variable pmf that calls the class member function, whether it calls the strcpy () function or the strcat () function. The pointer to the external original function and the pointer to the class member function are very different. The latter must point to the Host Object of the called function. Therefore, in addition to member pointers, there must also be valid objects or object pointers.

The following is an example. Assume that A has two instances, and the member function pointer supports polymorphism. In this way, the member pointer is dynamically processed when calling the virtual member function ). Note: constructor and destructor cannot be called. Example:

 
 
  1. A a1, a2;
  2. A * p = & a1; // create A pointer to
  3. // Create a pointer to a member and initialize it.
  4. Void (A: * pmf) (char *, const char *) = & A: strcpy;
  5. // To bind a member function to pmf, you must define the call object.
  6. // You can use the * number for guidance:
  7. Void dispatcher (A a, void (A: * pmf) (char *, const char *))
  8. {
  9. Char str [4];
  10. (A. * pmf) (str, "abc"); // bind a member function to pmf
  11. }
  12. // Or use the pointer expression of a to point to the member pointer:
  13. Void dispatcher (A * p, void (A: * pmf) (char *, const char *))
  14. {
  15. Char str [4]; (p-> * pmf) (str, "abc ");
  16. }
  17. // Function call method:
  18. Dispatcher (a, pmf); //. * Method
  19. Dispatcher (& a, pmf); //-> * Method

Advanced usage skills

The above is the basic knowledge of member functions. Now we will introduce its advanced usage skills.

Member pointer Array

In the following example, an array containing two member pointers is declared and the member function address of the class is allocated to the member pointer:

 
 
  1. PMA pmf[2]= {&A::strcpy, &A::strcat}; 

Such arrays are useful in menu-driven applications. After selecting the menu item, the application will call the corresponding call back function, as shown below:

 
 
  1. Enum MENU_OPTIONS {COPY, CONCAT };
  2. Int main ()
  3. {
  4. MENU_OPTIONS option; char str [4];
  5. // Read from external resources
  6. Switch (option)
  7. {
  8. Case COPY:
  9. (Pa-> * pmf [COPY]) (str, "abc ");
  10. Break;
  11. Case CONCAT:
  12. (Pa-> * pmf [CONCAT]) (str, "abc ");
  13. Break;
  14. //...
  15. }
  16. }

Const type member functions

The type of the member pointer should be the same as that of the member function. In the above example, pmf can point to any function of A, as long as the function is not of the const type. As shown in the following figure, if the touppercase () address is assigned to pmf, a compilation error occurs because the touppercase () type is const.

 
 
  1. Class
  2. {
  3. Public:
  4. Void strpcy (char *, const char *);
  5. Void strcat (char *, const char *);
  6. Void touppercase (char *, const char *) const;
  7. };
  8. Pmf = & A: touppercase; // error, Type Mismatch
  9. // The solution is to declare a const type member pointer:
  10. Void (A: pcmf) (char *, const char *) const;
  11. Pcmf = & A: touppercase; // now you can

Some bad compilers allow a non-const type member pointer to a const type member function. This is not allowed in Standard C ++.

Conclusion

It may be a bit confusing: The member pointer is not a real pointer. Traditionally, a pointer is an integer that stores the address pointing to a variable or function. A member pointer is a composite data structure that contains several data members. The complexity of member pointers makes it difficult to get started. However, once you have mastered its syntax, you can feel that it is an essential tool for calling call-back functions in event-driven and multi-threaded applications.

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.