Pointer to function (i) __ function

Source: Internet
Author: User

The reason why I want to write a function pointer is the extensive use of the function pointer in C + + programming, while for some beginners, the use of function pointers may be somewhat confusing, and once the function pointer is used at the appropriate time, the code will be concise and powerful. This article describes the basic part of the function pointer, and the application of the complex function pointer will be introduced in the next article. a pointer to a normal function

Let's take a look at a function:

int Sum (int a, int b)
{return
  a + b;
}

This function can be invoked in such a way that

Sum (1, 2);

To represent a pointer to a function, you can either use &sum or remove the address operator & before Sum, and for ordinary functions, the address operator & is Optional.

function pointer variables and function pointer types are described below:

1. function pointer variable

Int (*fnname) (int, int);           Declares a function pointer that can interpret fnname as a newly defined variable
fnname = ∑			   Assign the address of the SUM function to it
(*fnname) (3, 5);		   and call SUM (3, 5) is the same effect

Line 1th declares a function pointer variable and, if in doubt, can interpret fnname as a newly defined variable. Declaration format for function pointer variables:

return type (* function pointer variable) (parameter list);

The 2nd line assigns the SUM function pointer to it, noting that there are only two function pointer parameter types, the return value type is exactly the same to assign a value, note that the difference between modifier const,& and so on can cause the assignment to fail.

Line 3rd is called, calling the format:

(* function pointer variable) (argument list);

2. function pointer type

The Declaration of function pointer variables is described earlier, so how do function pointer types are declared? Adding a typedef in front of the function pointer declaration becomes the function pointer type definition.

typedef int (*fntype) (int, int);   Declare a function pointer type
fntype FB = ∑				    Defines a variable of type Fntype and assigns a value
(*FB) (3, 5);					    Function call

Line 1th declares the type of the function pointer, and Fntype is the type of the new declaration, which is the type of the function pointer.

Line 2nd defines a variable of type Fntype and assigns the SUM function address to it.

Line 3rd is a function call. The variables and types of the function pointers have been previously understood, and the following code deepens the understanding:

int Sum (int a, int b)
{return
  a + b;
}

typedef int (*fntype) (int, int);
int Fun1 (fntype ft, int x, int y)
{return
  (*ft) (x, y);
}
function pointers can be defined in the parameter list, using
int Fun2 (int (*FN) (int, int), int x, int y) {return
  (*FN) (x, y) in  
the function body;}

int main ()
{
  cout << Fun1 (&sum, 2, 3) << "";  Output 5
  cout << Fun2 (&sum, 3, 4) << "\ n";//Output 7 return
  0;
}

On the normal function of the pointer to learn it here, simple:), the following is to learn the class member function pointer. two pointers to class member functions First look at the following class:

Class num
{public
:
  num () {n_ = 0;}
  void Inc (int n);
  void Dec (int n);
  static int Sub (int a, int b);
Private:
  long n_;
};

There are ordinary member functions in this class, and there are static member functions, regardless of which function, the function pointer representation is:

& Class Name:: Function name

The pointers to the three member functions of the NUM class are:

&Num::Inc;

&num::D EC; &num:: Sub;

1. Pointers to normal member functions

When you declare a pointer to a class member function, you need to use::* symbol, the class name on the left, and the member function pointer name on the right:

return type class Name::* member function pointer (argument list);

Call to use the. * or->*, the left side is a reference or pointer to the class object, and the right side is the member function pointer:

(Object name. * member function pointer) (argument);

Or

(object pointer->* member function pointer) (argument);

code example:

int main ()
{
  Num obj;
  void (Num::* MF) (int);    To declare a pointer to a member function MF
  = &Num::Inc;           Assigned value  
  (OBJ.*MF) (1);             Call

  //member function pointer type
  typedef void (Num::* mt) (int);
  MT fn = &num::D EC;
  (OBJ.*FN) (2); 

  return 0;
}

Note that the sub is a static member function whose pointer declaration is not the same as a non-static member function, which looks at the pointer to a static member function. 2. Pointers to static functions

Int (*SMF) (int a, int b); Note the
SMF = &Num::Sub;
cout << (*SMF) (6, 7); 	   Call the same way as the normal function call in the previous section

As you can see, the static member function pointer variable, type declaration is consistent with the normal function.

3. pointer to virtual function

First code:

Class base{public
:
  virtual void F () const
  {
    cout << "I am" base\n ";
  }

  typedef void (Base::* fnptr) () const;

Class Derived:public base{public
:
  virtual void F () const
  {
    cout << "I am The derived\n";
  }
};

int main ()
{
  base::fnptr fp = &Base::F;
  Base base;
(BASE.*FP) ();
  Derived Derived;
  (DERIVED.*FP) ();

  return 0;
}

Output results:

I am Thebase

I am thederived visible, the result of a virtual function's pointer invocation is the same as calling a virtual function directly, and the function address of the virtual function is the address of the function that the object dynamically binds to.

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.