?
Solved

Callback function

Posted on 1999-01-11
4
Medium Priority
?
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 2

Accepted Solution

by:
AdrianBrown earned 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

752 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