C ++ member function pointer Application)

Source: Internet
Author: User

In C ++, member pointers are the most complex syntax structures. 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:

Void (* PF) (char *, const char *);

Void strcpy (char * DEST, const char * Source );

PF = strcpy;

A pointer to a Class A member function is declared:

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:

Class
{

Public:

Void strcpy (char *, const char *);

Void strcat (char *, const char *);

};
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 *:

Typedef void (A: * PMA) (char *, const char *);

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:

A A1, A2;

A * P = & A1; // create a pointer to

// Create a pointer to a member and initialize it.

Void (A: * PMF) (char *, const char *) = & A: strcpy;

// To bind a member function to PMF, you must define the call object.

// You can use the * number for guidance:

Void DISPATCHER (A, void (A: * PMF) (char *, const char *))

{

Char STR [4];

(A. * PMF) (STR, "ABC"); // bind a member function to PMF

}

// Or use the pointer expression of a to point to the member pointer:

Void DISPATCHER (A * P, void (A: * PMF) (char *, const char *))

{

Char STR [4]; (p-> * PMF) (STR, "ABC ");

}

// Function call method:

DISPATCHER (A, PMF); //. * Method

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:

Pma pmf [2] = {& A: strcpy, & A: strcat };
That is
Void (A: * PMA [2]) (char *, const char *) = {& 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:

Enum menu_options {copy, Concat };

Int main ()
{
Menu_options option; char STR [4];
// Read from external resources
Switch (option)
{
Case copy:

(Pa-> * PMF [copy]) (STR, "ABC ");

Break;

Case Concat:

(Pa-> * PMF [Concat]) (STR, "ABC ");

Break;

//...

}
}

  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.

Class
{

Public:

Void strpcy (char *, const char *);

Void strcat (char *, const char *);

Void touppercase (char *, const char *) const;

};

PMF = & A: touppercase; // error, Type Mismatch

// The solution is to declare a const type member pointer:

Void (A: pcmf) (char *, const char *) const;

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

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.