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
Solved

Callback function

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.

856 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