Pointers to member functions of C + + classes and usage of Mem_fun adapters

Source: Internet
Author: User

I. Common function pointers

Let's first look at one of the simplest functions:

void fool (int  a) {    << a<< Endl;}

Then its function pointer type is:

void (*) (int)

We can test it this way:

void (*pfunc) (int) = &foo; // here PFunc is a pointer pFunc (123);

This will print out the integer 123;
To simplify, we can use typedef:

void (*pfunc) (int);

Here we want to explain:

Here the Pfunc is a null return value, a formal parameter is an int type of function type of a nickname, that is, PFUCN is a type, we can put pfunc analogy int,double.

Then we can use it like this:

#include <iostream>#include<string>using namespacestd;voidFool (inta) {cout<< a<<Endl;}intMainintargcConst Char*argv[]) {typedefvoid(*pfunc) (int); PFunc FC= &fool;//FC is equivalent to a variable (function pointer)FC (1223); return 0;}

The above is the basic application of function pointers.

member functions of Class II

So, how do we declare and apply the member functions of the class?

Observe the following classes:

class foo{    public:        void Foo (int  a)        << a < < Endl;}                 Static void Bar (int  a)        << a << Endl;};

We call this in the main function:

void (*pfunc) (int) = &Foo::foo;

You will find that the compiler will report an error like this:

Error:cannot convert 'void (Foo::*) (int) ' to 'void (*) (int in Initialization

From the compilation error above, we learned that the function pointer type of foo is not the Void (*) (int) We expected, and that the function pointer type of foo is actually a void (foo::*) (int) type.
The reason is simple: the member function of the class, contains an implicit parameter this, in Foo for example, here Foo actually has two parameters, one is the this pointer, and the other is the int variable.

We tried to use the void (Foo::*) (int) type with the following code:

void (Foo::* pFunc2) (int) = &Foo::foo;

How do we use it? There are two ways of doing this:

// object is passed   .  Method invocation (F.*PFUNC2) (12345);
Foo *PF = &f; // The object pointer is called by way of (PF->*PFUNC2) (123124);


Here we also notice that the above class contains a static member function, what is the characteristic of it?

void (*pfunc) (int) = &Foo::bar; // OKpFunc (123);

We call the above code in the main function and find that it can run successfully. This is due to the characteristics of the static member:

Static declares this function to be all of this class, not an object of a single class. So we can use the static member function just like the normal function.

The complete code is as follows:

#include <iostream>#include<string>#include<typeinfo>using namespacestd;classfoo{ Public:        voidFoointa) {cout<< a <<Endl;} Static voidBarinta) {cout<< a <<Endl;} };intMainintargcConst Char*argv[]) {//veersion 1//void (*pfunc) (int) = &Foo::foo;//Error//there are two formal parameters for Foo::foo: An implicit, an int//the formal parameter of void (*PFUNC) (int) has only one//PFunc (123);//Version 2    void(*pfunc) (int) = &Foo::bar;//OKPFunc (123); //This shows that the parameters of the static member function do not have an implicit argument (this object)//fix 1--> To add a parameter to it    void(Foo::* pFunc2) (int) = &Foo::foo;    Foo F; (F.*PFUNC2) (12345); Foo*PF = &F; (PF-&GT;*PFUNC2) (123124); return 0;}


Three, Mem_fun adapter:

Header file:<functional>

Since foo contains an implicit parameter, can it be converted? We use the STL Mem_fun, which is a function adapter.

Foo F; // void (Foo::*) (int)->void (*) (Foo*,int) (Mem_fun (&foo::foo)) (&f,123);

The specific role of Mem_fun is a conversion that converts a member function pointer of void (Foo::*) (int) to void (foo*, int), which is a pointer to a free function type that can be invoked as freely as a static member function.

Pointers to member functions of C + + classes and usage of Mem_fun adapters

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.