Solved

Callback function

Posted on 1999-01-11
4
231 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now