1 Explanation of function pointers

Source: Internet
Author: User

Today, for the first time, the article about C + +, mainly the recent projects to use Boost::asio as a network library, and Boost::asio to use a lot of boost::bind functions, and boost::bind to use the free function pointers and member function pointers, So this string down, I'm going to talk about the function pointer this thing.

One, free function pointer

This free function should be the corresponding member function that is not in the class of the function bar, English is called free function, I am hard to turn, welcome students to correct the face. Its pointers are easy to write and are divided into two ways:

(a) First declare the function pointer type, and then define the function pointer instance.

For example, I have a free function:

. h:

Inline double afreefun (double param);

. CPP:

#include <iostream>

Double Afreefun (double param)

{

std::cout<< "Afreefun function:" <<param <<std::endl;

return param;

}

It's simple, it has a double-type parameter, and a double-type return value. It is important to note that when you write a free function in a class file, you remember to use inline or the static keyword, or you will be prone to duplicate definitions. I declare a function pointer of this function type in the main function:

int main (int argc, char *argv[])
{
typedef double (*freefunpoint) (double);
Freefunpoint pointinstance = Afreefun;
std::cout<< "Freefunpoint function point:" <<pointinstance (5) <<std::endl;
}

See, the function pointer type is defined using TypeDef, and the format is

typedef return Value (* function pointer type name) (parameter list);

Then assign the function name of the free function to an instance of such a function pointer--pointinstance, in the following format:

function pointer type name function pointer instance = Address of the corresponding type function;

This example can then be used just like this free function.

Here, the function name and array name can be used as pointers to the memory, but the actual correct usage is as follows, but the wood is necessary.

int main (int argc, char *argv[])

{

typedef double (*freefunpoint) (double);

Freefunpoint pointinstance = &aFreeFun;

std::cout<< "Freefunpoint function point:" << (*pointinstance) (8) <<std::endl;

}

That is, the function name takes the address to get the function address, when used to remove the address before use.

If you just look at this, you will think this is what method ah, I can not directly call this free function, around such a big circle what to do, this benefit will be mentioned later, it is to let the function also become an instance, in your program passed.

(ii) directly define the function pointer instance.

or using the free function above, this time in the main function, write this:

int main (int argc, char *argv[])
{
Double (*pointinstance3) (double) = Afreefun;
}

Without typedef, do not create a function pointer type, directly on the function pointer, in the form of

return Value (*) (argument list) = Address of the corresponding type function;

Of course there is the previous one to take the address and release the pointer version, do not write it.

(iii) use in C++11

It has been said that there are only two kinds of, here again, hehe, now c++11 concept so fire, it seems that after the c++11, C + + will become a door regardless of memory management, saving temporary variable generation time, with the language of the thread. So if there is a c++11, you can do it using the using method, as follows:

int main (int argc, char *argv[])
{
Using FreeFunPoint2 = Double (*) (double);
FreeFunPoint2 pointInstance2 = Afreefun;
std::cout<< "Freefunpoint function Point2:" <<pointinstance2 (6) <<std::endl;
}

In this case, the concept of a function pointer is easy to understand, and FreeFunPoint2 represents a double (*) (double) of this type of function pointer. The format is as follows:

Using function pointer type name = return value (*) (parameter list);

The content of the free pointer is introduced here, learned this, will be able to use the design mode of the strategy mode!

Second, the member function pointer

The member function pointer here is a little more troublesome than the free function pointer, mainly to grasp a concept: class, NAMESAPCE, struct, etc. are the concept of the domain, they add: "Can access their internal static public content, and functions, although also the domain, But for the outside world should be a closed system, is inaccessible to.

I first write a class that operates on its member functions.

. h

Class Memberfunpointstudy

{

Public

Memberfunpointstudy ();

void Usememberfunc ();

Double Amemberfunc (double param);

};

Where the function amemberfunc is the function I'm about to manipulate, and the function Usememberfunc (), I'll write out a method that declares a function pointer and a function pointer instance in the same class.

. cpp

Double Memberfunpointstudy::amemberfunc (double param)

{

std::cout<< "Amemberfunc function:" <<param <<std::endl;

return param;

}

First, let's talk about the method of declaring a function pointer and a function pointer instance in the same class, as well as three ways to use a function pointer.

(a) First declare the function pointer type, and then define the function pointer instance.

void Memberfunpointstudy::usememberfunc ()

{

typedef double (memberfunpointstudy::* Usememberfunc) (double);

Usememberfunc pointinstance = Amemberfunc;

std::cout<< "Memberfunpoint function point:" << ((*this). *pointinstance) (1) <<std::endl;

std::cout<< "Memberfunpoint function point:" << (This->*pointinstance) (3) <<std::endl;

std::cout<< "Memberfunpoint function point:" << ((*this). Pointinstance) (3) <<std::endl;

}

To say the correct usage first, the method of declaring the member function pointer type of this class is as follows:

typedef return value (class name::* function pointer type name) (parameter list);

The declaration defines a specific instance of a function pointer as follows:

function pointer type name function pointer instance = Address of the corresponding type function;

Or

function pointer type name function pointer instance = & address of the corresponding type function;

We find that declaring this function pointer type is not much different from the set of free functions previously spoken, just adding the domain of the class, and defining the function pointer instance here is exactly the same. Class name This field indicates that the instance created by this function pointer type can only receive function pointers in the corresponding format in this class field. Privately, it seems that it does not support a function pointer that can receive the corresponding format in any class domain when I write the following

typedef double (*usememberfunc) (double);

Usememberfunc pointinstance = Amemberfunc;

, the following error is reported in the second line:

Error:cannot convert ' Memberfunpointstudy::amemberfunc '

From type ' double (memberfunpointstudy::) (double) '

To type ' Usememberfunc {aka Double (*) (double)} '

It's meant to be said in time, amemberfunc the type of the function is ' double (memberfunpointstudy::) (double) ', cannot convert it to a double (*) (double), So this fully illustrates that the domain of the member function is very important, once it belongs to a class, it is no longer a function, but a function of a class, its type must contain the type of the class. There's a problem with the red mark here, and we'll talk about it right away.

But there is a big difference when it comes to use. As you can see, this is used only after the function pointer instance is dereferenced, and it is called by a owning class object or class pointer, and the format of the call is also relatively fixed. For:

(Class object. * Function pointer instance) (arguments are passed in);

Or

(class object pointer->* function pointer instance) (arguments are passed in);

Can also be seen, and the free function there is not much, that is, the previous specified class object, and the call is also used in the above-mentioned method of lifting pointers, and then the use of parentheses. It is important to note that the de-pointer is not optional here, but must be selected, and the following error will be reported if it is run in the comment section of the function:

Error: ' Class Memberfunpointstudy ' has no member named ' Pointinstance '

See no, the compiler does not recognize that this pointinstance is a function pointer ah, it is just silly thought he is a member variable, and then weak weak to tell you the wood has this member variable good panic how to do! So you have to use a solution to the reference, at this point the compiler looks for pointers to the content, found that the original Amemberfunc function ah. Then, you can use it. So privately thought, add this *, add no that class domain::, add not add the class object, add not spicy class object pointer, that is, only four difference, so easy, no longer worry about the callback function write bad.

(ii) directly define the function pointer instance.

void Memberfunpointstudy::usememberfunc ()

{

Double (memberfunpointstudy::* PointInstance2) (double) = &aMemberFunc;

std::cout<< "Memberfunpoint function point:" << ((*this). *pointinstance2) (1) <<std::endl;

std::cout<< "Memberfunpoint function point:" << (THIS->*POINTINSTANCE2) (3) <<std::endl;

}

I feel that needless to say, a look will be, add to & is the same.

(iii) use in C++11

void Memberfunpointstudy::usememberfunc ()

{

Using USEMEMBERFUNC2 = Double (memberfunpointstudy::*) (double);

UseMemberFunc2 pointInstance3 = Amemberfunc;

std::cout<< "Memberfunpoint function point:" << ((*this). *pointinstance3) (1) <<std::endl;

std::cout<< "Memberfunpoint function point:" << (This->*pointinstance3) (3) <<std::endl;

}

This is the use of the method, but also minutes to master Ah, but see no, I marked red above, said that the following will be the part of a double (memberfunpointstudy::*) (double) Here, I tried, do not add * Even the IDE's own view can not pass, Not to mention the compiler, so the error message provided by the compiler is also important. The compiler I'm using is mingw492_32.

The three methods above are examples of function pointers created for this class, and finally, let's look at how to create them in other classes, and I'll write directly to the main function.

(iv) Defining a function pointer instance in another class

As shown below:

int main (int argc, char *argv[])

{

typedef double (memberfunpointstudy::* Usememberfunc) (double);

Usememberfunc pointinstance = &MemberFunPointStudy::aMemberFunc;

Double D = (Memberfunpointstudy (). *pointinstance) (2);

}

And the previous difference is to give the class address, and then through:: Find its function, I have tried, only this kind of writing,& can only be added in front of the class name, and the front of the function can not be added &, the reason can actually think of,& Memberfunpointstudy is already the address of the class, do not need and can not find the address of amemberfunc, directly through the domain can be found.

Also see no, there is a need for a class object to invoke the dereference of a pointer to a class member variable.

Finish, eat, scatter.

1 Explanation of 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.