Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Direct Function Calling

Posted on 1997-02-22
1
Medium Priority
?
206 Views
Last Modified: 2010-04-10
I need to be able to call a function given only it's address.  I want to be able to pass that address into one function and then call the function that address coresponds to.

void foo(int i){
  //Some Code Here
}
void foo2(int i){
  //Some Code Here
}

void caller(void *tmp){
  void *ptr = tmp; //Make ptr = address of foo
  ptr(1);
  }

void main(void){
  caller(foo);
  caller(foo2);
}

I realize this does not work but I want to do something similar to this.  Is there an easy way to do it or do I need to use assembly?
I known I could use classes and virtual functions but would like to do it this way.

Thanx
lordx@wam.umd.edu
0
Comment
Question by:lordx
[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
1 Comment
 
LVL 3

Accepted Solution

by:
os012897 earned 140 total points
ID: 1162110
As far as I can remember it should work like that:

.....
...
.

void caller (void *tmp)

{    (*tmp) (1);   //dereference function + call it with argument
}

void main (void)

{    caller (&foo);  //get address of foo
     caller (&foo2); //get address of foo2
}


Regards,

       os

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

722 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