473,394 Members | 1,746 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.

Call up functions

Hi everyone,

I have a question that is tricking me.

I need to do something like this:
-----------------------------------------------------------

Expand|Select|Wrap|Line Numbers
  1. int a;
  2.  
  3.  
  4. void* function(const &function){
  5.     //execute the code of the function here that i sent the address
  6. }
  7.  
  8. int main(){
  9.        a = 0 ;
  10.  
  11.        call(&myFunction);
  12.  
  13.     cout << a ;
  14. }
  15.  
  16.  
----------------------

I don't know if i explained it well but what I need is a function to call another functions by its adress. For example, I use call(&function2); then it runs all function2()

.
Feb 24 '08 #1
1 1444
weaknessforcats
9,208 Expert Mod 8TB
You need a function pointer.

That is, your call() needs an argument that is a pointer to a function that has the correct arguments and return type.
Expand|Select|Wrap|Line Numbers
  1. void AFunction(int x, float y)
  2. {
  3.     //etc...
  4. {
  5.  
A pointer to this function would be:
Expand|Select|Wrap|Line Numbers
  1. void (*fp)(int, float);
  2.  
Then you can assign the address of AFunction to this pointer:
Expand|Select|Wrap|Line Numbers
  1. fp = AFunction;
  2.  
The name of a function is the address of the function so you not code &AFunction.

Then, your call() just has to have a function pointer argument:
Expand|Select|Wrap|Line Numbers
  1. void call(void (*fp)(int, float))
  2. {
  3.             //etc...
  4. }
  5.  
Finally, in main() you can make the call:
Expand|Select|Wrap|Line Numbers
  1. call(AFunction);
  2.  
Remember, the argments and return type declared in your function pointer must match those of the actual function or you cannot put that function's address into the pointer.
Feb 24 '08 #2

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

Similar topics

7
by: Bruce W...1 | last post by:
I'm a PHP newbie coming from experience with ASP.NET. I want to have a separate PHP file to support each HTML PHP page. This would be the equivalent of an ASP.NET code-behind file but using PHP....
2
by: Greg Chapman | last post by:
I am at my wit's end trying to get information out of Streamline.net's support dept about my problem. They reply quickly enough, but seem to try and give out the least possible amount of info each...
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
6
by: Philip Warner | last post by:
I have a set of classes: C (base) C1 (inherits C) C2 (inherits C) ... Cn (inherits C) (the C1..Cn also have subclasses, but thats not really relevant to the question)
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
15
by: AmmarN | last post by:
Hi all, I have a c++ programme that intakes a binary file and performs various operations on it. I am not gonna get in to the details of the operations, currently i specify the order in which the...
5
by: GCRhoads | last post by:
I have some templated functions and I want to write a call wrapper class for it. I also want to pass the function object from this class to some functions that will then call the function...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
9
by: Andrew Poulos | last post by:
Say I have this: var foo = function(x, y) { return x + y; }; var bar = function(fname, param1, param2) { /* * how do I call the function "foo" * and pass parameters to it?
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.