Implements an array of class member function pointers, __ functions

Source: Internet
Author: User

///to implement an array of class member function pointers, see the code below, and also note that static array members can not declare array size within a class
class Cmymath
{
Public:
    int ADD (int A, int b);
    int Sub (int a, int b);
   ///matharr is an array in which the array holds the pointer, and the pointer is the function pointer
   ///function is a Cmymath class member function
    ///So Matharr is an array of Cmymath class member function pointers,
    static int (cmymath::* matharr[]) (int, int); br>    int Calc (void);
       
};
implementation of///array
Int (cmymath::* (cmymath::matharr[])) (int, int) =
{
    &cmymath::add,
    &cmymath::sub,
    NULL,
};

int Cmymath::add (int a, int b)
{
return a+b;
}
int cmymath::sub (int a, int b)
{
return a-b;
}
int Cmymath::calc ()
{
Class called Outside the class call, which is called directly from the
Return (This->*matharr[0]) (10, 30);
}

Cmymath Math;
Call within the class
int c = Math. Calc ();
class, because the function is a function within the class, so add the class's variable name to the pointer before calling it math
c = (math.* (cmymath::matharr[1])) (50, 9);

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.