473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Function pointers and callback functions

Hi,

I understand the concept of function pointers.
I am a little confused with the call back functions.

1.If a function is invoked using a function pointer, then does it mean the function invoked is a callback function?

2. Some say that the calling convention used for a callback function is different(__stdcall,PASCAL).However, I see that such calling conventions are used only for callback functions registered with the OS.I dont see it being used for application level callbacks.

3.Some say that any function invoked using a function pointer, would automaticaly be a callback only when the call is happening between a dll and third party.

Which of the above statements are correct?

Any help in this regard would be really appreciated.

Reagrds,
kikivenkat
Sep 18 '07 #1
1 2388
weaknessforcats
9,208 Expert Mod 8TB
OK, here you go:

1) all functions are called by address.
2) the name of the function is the address of the function.
3) a function pointer is just a pointer that can contain the address of a function.
4) when you use the function pointer to call the function:
Expand|Select|Wrap|Line Numbers
  1. fptr(3);        ??
  2. fptr(3,4);     ???
  3. fptr(3,4,"Hello"); ???
  4.  
the function whose address is in the function pointer better have the correct arguments and return type. That is why when you initialize a function pointer you can only use functions with arguments and return type that match rthe pointer definition:
Expand|Select|Wrap|Line Numbers
  1. void MyFunction(int, int);
  2. void (*fptr)(int, int);
  3.  
  4. fptr = MyFunction;   //OK
  5.  
  6. fptr(3,4);     //calls MyFunction
  7. fptr(3);        //COMPILER ERROR - incorect arguments
  8. fptr(3, 4, "Hello");        //COMPILER ERROR - incorect arguments
  9.  
Now a callback function is just passing a function pointer to a function that does the callback. That means using a function pointer as a function argument. And that means the arguiments and return type have to be known at the time the function doing the callback was written - perhaps years before you come along. Therefore, the callback function pointer has pre-defined arguments and return types. That means your function, to be used as a callback, must conform to the callback function spec.

Here is one from one of the Windows APIs:
HRESULT __stdcall myCallbackFunctionName(
DRM_STATUS_MSG msg,
HRESULT hr,
VOID* pvParam,
VOID* pvContext
);
That means your function must have these arguments, return type, and calling convention as this pattern. Otherwise, you can't use it as a callback.
Sep 24 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Chris Morley | last post by:
Hi, I have always done my C++ class callbacks with the age old 'using this pointer in parameter of the class's static callback function' and typecasting it to get the specific instance. However...
3
by: Jan-Henrik Grobe | last post by:
Hallo, I am coming to this newsgroup with a very strange Problem. I have two c++ files A.cpp and B.cpp....In A.cpp I am creating an openGL window and in B.cpp I have stored the callback...
1
by: Mitchell Hughes | last post by:
Hi, I have code from a previous project that I've packaged into a dll in c++. I'm now trying to call functions within that c++ dll from my c# code. I've managed to get it to work for ints and...
45
by: noridotjabi | last post by:
What is the purpose of the function pointer? Why do you need a pointer to a function. I cannot really think of any application where this is the only or even easiest solution to a problem. I'm...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
1
by: pheres | last post by:
Hi, I'm trying to pass pointers to member functions around in my code. The test with pointers to non-member function works fine: Code: void callOut( void (*callback)() ) { callback();
6
by: JDT | last post by:
Hi, Can we pass a member function in a class as a callback function? Someone instucted me that I can only use a static functon or a global function as a callback. Your help is appreciated. JD
6
by: jmDesktop | last post by:
In a function that takes another function (function pointer) as a argument, or the callback function, which is the one that "calls back"? I'm having a hard time understanding the language. Am I...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.