Solved

Callback function

Posted on 1999-01-11
4
237 Views
Last Modified: 2010-04-02
in one of my method i must use a callback function.
the functin is passed like a pointer, how i can use them
Register(void *CallbackFunction,char *name)
{
  i =  (CallbackFunction*)(name,Number);??
}
0
Comment
Question by:Zircona
4 Comments
 
LVL 2

Accepted Solution

by:
AdrianBrown earned 100 total points
ID: 1181852
You need a pointer to a function like

void MyFunc(int a, char b, int c)
{
  // Whatever
  printf("hello");
}

void (Function *)(int, char, int);

void main(void)
{
  Function = &MyFunc

  // Call MyFunc
  Function(10, 3, 2);
}

I think ive got the brackets and address markers in the right place.  If this isnt correct or not what your asking let me know

Hope this helps

0
 
LVL 22

Expert Comment

by:nietod
ID: 1181853
For greater programmer ease and safety, you might want to use virtual functions instead of function pointers.  There is no need for funciton pointers in C++ (unless the project must interface with non-C++ code, like the OS.)
0
 
LVL 1

Expert Comment

by:shrif
ID: 1181854
I just want to echo wnat nietod has said.  For those of you who want to write more object oriented code, when you come across a need for a function pointer -- step back and think about the problem and you'll see that a virtual function will fit the bill very nicely.
0
 

Expert Comment

by:Sergio_Ravasio
ID: 13960956
OK, could you pls give me an example how do you write the AdrianBrown code (see on top of the page) by using virtual functions?
thanks in advance
Sergio
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question