Class function pointer Array application instance

Source: Internet
Author: User
The class member function pointer is similar to the class data member pointer. In development and Apache source code, there are exquisite applications of function pointer arrays.
This article mainly describes how to use function pointer arrays to reduce a large amount of repetitive work and improve code readability and efficiency.
// Declare the class containing the public member function
Class X
{
Public:
Int value;
Int total;
Int count;
Void setvalue (INT v) {value = V ;}
Void settotal (INT t) {Total = T ;}
Void setcount (INT c) {COUNT = C ;}

};

FP function [] =
{
& Amp; X: setvalue,
& Amp; X: settotal,
& Amp; X: setcount
};

// Define and initialize an array pointing to the class member function pointer
// Define a return value as void, with an int pointing to a function pointer
Typedef void (X: * FP) (INT );

// Create a Global Object
X;

// Use a pointer to a class member function
Void setmember (void (X: * Foo) (INT), int V)
{
// Pay attention to using the function pointer of the object. * You can-> *. For example: (A. * function [0]) (INT)
(A. * Foo) (v );

}
Void main ()
{
// Use the global object and pointer Array
Setmember (function [0], 5 );
Setmember (function [1],-13 );
Int start = 10;
Setmember (function [2], start );
}

 

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.