C + + Common function pointer and member function pointer instance parsing _c language

Source: Internet
Author: User
Tags mul

C + + 's function pointer (function pointer) is called indirectly by a pointer to a function. It is believed that many people use more of the function pointers to the general function, while the function pointer to the class member function is quite unfamiliar. In this paper, the common function pointer and member function pointers of C + + are analyzed with examples.

A common function pointer

Normally, the function pointer refers to a pointer to a general normal function. Like other pointers, function pointers point to a particular type, and all functions that are used by the same pointer must have the same parameter type and return type.

Int (*PF) (int, int);  declaring function pointers

Here, pf points to the type of function that is int (int, int), the function argument is two int, and the return value is int type.

Note:the parentheses at the ends of the *PF are essential, and if this pair of parentheses is not written, PF is a function that returns a value of int pointers.

#include <iostream> 
#include <string> 
using namespace std; 
 
typedef int (*pfun) (int, int); typedef a Type 
 
int add (int a, int b) {return 
  a+b; 
} 
 
int MNS (int a, int b) {return 
  a-b; 
} 
 
String merge (const string& s1, const string& s2) {return 
  s1+s2; 
} 
 
int main () 
{ 
  Pfun pf1 = add;  
  cout << (*PF1) (2,3) << Endl; Call add function 
  PF1 = MNS; 
  cout << (*PF1) (8,1) << Endl; Call MNS function 
  string (*PF2) (const string&, const string&) = merge; 
  cout << (*PF2) ("Hello", "World") << Endl; Call the merge function return 
  0; 
}

As with the example code, declaring function pointer variables directly is tedious and cumbersome, so we can use typedef to define our own function pointer types. In addition, the function pointer can also serve as the parameter type of the function, and the argument can use the function name directly.

Second, member function pointers

A member function pointer (pointer) is a pointer that can point to a non-static member function of a class. A static member of a class does not belong to any object, so there is no special pointer to a static member, and a pointer to a static member is no different from a normal pointer. Unlike a normal function pointer, a member function pointer must specify not only the parameter list and return type of the target function, but also the class that the member function belongs to. Therefore, we must add classname before *:: To indicate that the currently defined pointer points to the ClassName member function:

Int (A::* PF) (int, int);  Declaring a member function pointer 

Similarly, here A::* the brackets at both ends of the PF are also essential, and if not, PF is a function that returns a type a data member (int) pointer. Note: Unlike a normal function pointer, there is no automatic conversion rule between a member function and a pointer to that member.

PF = &A::add;  Correct: You must explicitly use the FETCH operator (&) 
pf = A::add;  Error 

When we initialize a member function pointer, it points to a member function of the class, but does not specify the object to which the member belongs-until the member function pointer is used to provide the object to which the member belongs. The following is an example of the use of a member function pointer:

Class A; typedef int (A::* pclassfun) (int, int); member function pointer type class a{public:int add (int m, int n) {cout << m << "+" << n << "=" & 
    lt;< m+n << Endl; 
  return m+n; 
    int MNS (int m, int n) {cout << m << "-" << n << "=" << m-n << Endl; 
  return m-n; 
    int mul (int m, int n) {cout << m << "*" << n << "=" << m*n << Endl; 
  return m*n; 
    int dev (int m, int n) {cout << m << "/" << n << "=" << m/n << Endl; 
  return m/n; 
  int call (pclassfun fun, int m, int n) {//Class internal interface return (This->*fun) (M, n); 
 
} 
}; 
int call (A obj, pclassfun fun, int m, int n) {//Class Outer interface return (Obj.*fun) (M, n); 
  int main () {a A; 
  cout << "member function ' call ':" << Endl; 
  A.call (&a::add, 8, 4); 
  A.call (&a::mns, 8, 4); 
  A.call (&a::mul, 8, 4); A. Call (&a::d ev, 8, 4); 
  cout << "External function ' call ':" << Endl; 
  Call (A, &a::add, 9, 3); 
  Call (A, &a::mns, 9, 3); 
  Call (A, &a::mul, 9, 3); 
  Call (A, &a::d ev, 9, 3); 
return 0;

 }

As the example shows, we can use TypeDef to define the type aliases of member function pointers. In addition, we need to pay attention to how the function pointers are used: for normal function pointers, this is the use of (*PF) (arguments), because to invoke a function, you must first solve the reference function pointer, and the function call operator () has a higher precedence, so (*PF) parentheses are necessary; For member function pointers, The only difference is that you need to call a function on an object, so you just need to add a member accessor:

(OBJ.*PF) (arguments)     Obj is an object 
(OBJPTR->*PF) (arguments)   //ObjPtr is an object pointer  

Third, function table driver

A common use for normal function pointers and pointers to member functions is to store them in a function table. When a program needs to execute a particular function, it looks for the corresponding function pointer from the table and uses the pointer to invoke the corresponding program code, which is the application of the function pointer in the table-driven method.

Table-driven method (Table-driven approach) is a look-up table method for obtaining information. Usually, the logical judgment statement (If...else or switch...case) can be used to obtain the information, but as the data increase, the logical statement will become longer, the advantage of the table-driven method is reflected.

#include <iostream> #include <string> #include <map> using namespace std; 
Class A; 
 
typedef int (A::* pclassfun) (int, int); 
    Class a{Public:a () {//constructor, initialization table table["+"] = &A::add; 
    table["-"] = &A::mns; 
    table["*"] = &A::mul; 
  table["/"] = &a::d ev; 
    int add (int m, int n) {cout << m << "+" << n << "=" << m+n << Endl; 
  return m+n; 
    int MNS (int m, int n) {cout << m << "-" << n << "=" << m-n << Endl; 
  return m-n; 
    int mul (int m, int n) {cout << m << "*" << n << "=" << m*n << Endl; 
  return m*n; 
    int dev (int m, int n) {cout << m << "/" << n << "=" << m/n << Endl; 
  return m/n; 
  //lookup table, call the corresponding function int called (string s, int m, int n) {return (This->*table[s]) (m, n); } private:map<string, Pclassfun> table; 
 
function table}; 
  Test int main () {a A; 
  A.call ("+", 8, 2); 
  A.call ("-", 8, 2); 
  A.call ("*", 8, 2); 
  A.call ("/", 8, 2); 
return 0;

 }

The

is an example above, and the table in the example is implemented through a map (and, of course, arrays can be used). Table-driven use requires attention: first, how to check the table, read the correct data from the table, and the second is what to store, such as values or function pointers.

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.