"Go" C + + easy to mix knowledge point 2. The difference between a function pointer and a pointer function

Source: Internet
Author: User

We often use pointers in C + + development, the benefits of pointers are very small, can be easily used to achieve the desired function, of course, here also involves some basic concepts of pointers. Pointers are not basic data types, we can understand that he is a special type of object, he occupies a certain space, but the benefits are C + + so powerful deep-seated reasons.


Reprint Please specify source: Http://blog.csdn.net/elfprincexu


1. Pointer function, (__TYPE__ * func (void, int,)) As The name implies, he is a function, except for the reason that it is a pointer that is returned by a general function.
int* f (int, int); Returns a shaping pointer
int f (int, int);//Returns an integer number
The above two differences are only worth the difference, (note by the way, the return value is not overloaded functions, overloaded functions only according to the type and number of parameters, of course, the read-only function const is the basis for the evaluation of the overloaded function)
Of course, the pointer function, when used, must be the same as the caller's type, that is, the return value must be the same as the type of the left value.
int * a = f (5,67); Legal, same type
Summary: Pointer functions, relatively easy to understand, and the general function of the difference is only the return is worth the difference, call attention to return the type pointer.


2. function pointer (__type__ (* func) (void*, int))

The function pointer, as the name implies, is still a pointer, except that the pointer is special, and he has the same name as the other functions (return value type, parameter number and type)

Int (*pfunc) (int, float); Legal, defines a function pointer Pfunc, which has a return int type, with two parameters, one int, and another float;

We can simply understand that the function pointer is the same as the general function name, in fact, in general, the meaning of the function name is a functional entry address (pointer).

int getsum (int A, float b);

PFunc = getsum;//Legal, function names can also be interpreted as pointers

PFunc = &getSum; //Legal,& go to address symbol can be omitted

int x = (*pfunc) (3,50;//Legal, when invoked with a function pointer, we need to use registration to surround it,

void( *FUNCP) ();
voidFilefunc (), Editfunc ();
Main ()
{  
Funcp=filefunc;
( *FUNCP) ();
Funcp=editfunc;
( *FUNCP) ();
}  
voidFilefunc ()
{  
printf (filefunc\n);
}  
voidEditfunc ()
{  
printf (editfunc\n);
}  
The program output is:
Filefunc
Editfunc


Summary: The function pointer, essentially a pointer, but represents a function entry address, we can use this pointer flexibly using different functions.


in general, the function pointer is more commonly used, he can flexibly use different functions to achieve the results we want. For example, in common C + + applications, we tend to define a function pointer that implements the different implementations through inheritance.

classThreaduser
{     
Public:  
typedefvoid(threaduser::* entryptr) (void* arg);//defines a function pointer entryptr with an untyped pointer and a null value of void
}  
classThread

{  
Public:  
Thread (Threaduser&, Threaduser::entryptr,void* arg =0 );
    ...  
Private:  
pthread_t _thread;
pthread_attr_t _threadatrributes;
Thread::entrypt _entry;
threaduser* _user;
BOOL_done;void* _ARG;
Staticvoidentry (thread&);//Thread Entry function
Staticint_threadcount;
}  

Yi:
// define another function pointer for the Pthread_create service, Pthread_create thread entry function START_RTN requires this type of function

typedef void * (*entrypoint) (void*); Thread::thread (threaduser& u, threaduser::entryptr e, void* arg): _entry (E), _user (&u), _ Done (false), _arg (ARG)

{  
memset (&_thread,0, sizeof(_thread);
memset (&_threadattributes,0, sizeof(_threadattributes);
intThrcreateresult;
if( Thrcreateresult = Pthread_create (&_thread,&_threadattributes, (entrypoint) entry, This)) != 0) //This is used as the Argu of the entry function
    {  
Cerr <<"Pthread_create failed"<< errno << Endl;
    }  
Else
    {  
_started =true;
_threadcount + +;
    }  
returntrue;
}  

voidthread::entry (thread& t)//The entry function, the parameter is the thread object, on the above this
{  
(T._user->*t._entry) (T._ARG); //call the function that the function pointer points to
T._done =true;
}  

"Go" C + + easy to mix knowledge point 2. The difference between a function pointer and a pointer function

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.