Tuesday, July 14, 2009

Function pointer - C++ code section?

I came across following section of code %26amp; it is working with a highly complex system.


Can someone provide some explanation on this like which constructor is being called %26amp; where parameter pCallback is being sent %26amp; what is the meaning of void(*)(void*)%26amp; a::afoo mean?








===================================


a.h


static void afoo ( void * );





a.cc


void a::afoo ( void * pCallback )


--------------------------------------...


file b.h


b ( void );


b (


void (* pStatsUpdate ) ( void * ),


void *


);





file c.cc


new b (


( void


(*) ( void * ) ) %26amp; a::afoo,


this );


===================================

Function pointer - C++ code section?
It does not look sensible to me. The retyped text probably contains some syntax error, For example,





a.h


static voidafoo ( void * );





should be





a.h


static void a::afoo (void*);





or it must be part of a class or namespace declartaion as





class a {


// ...


static void afoo (void*);


//...


};





or





namspace a


{


//...


static void afoo (void*);


//...


};








It is also important to know in which part of the code these declarations and / or defintions take place.





This faily complex structure is either generatated by some auto code generator software, or some programmers that do not like to make their minds clear when they do programming.








The expression void(*)(void*)%26amp; a::afoo means a pointer to any function which returns a void and takes a void * as its only argument such as





void f (void * vp)


{


//...


}





a::afoo class method or namespace function obeys this signature, and therefore, it is qualified to be cast to this type.





I think yet there is another mistype with the %26amp; in this part unless it means a pointer to a pointer to the function (and not the pointer to function itself).
Reply:void(*)(void*)----means a functin pointer, that takes a pointer as parameter and does not have a return value............








the use of %26amp; is the change in function will in turn effect the actual arguements.....





for that paarmeter pCallBack, i think it will return the value to the constructor in a.h, provided you mention its location...

imperial

No comments:

Post a Comment