Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to define an Array of Function Pointers

Status
Not open for further replies.

rjsaul0

IS-IT--Management
Mar 2, 2006
6
Hi,

How would define an array of function pointers where the return type or arguments are different? For example,
int (*fn)()
void (*fn)(int)
char (*fn)(int, char)

This would be for building a dictionary of function calls.
Older C languages didn't strong typing so you could use int (*fn)() for any function call.
 
I'm not quite sure what you're trying to do, or what you'd use it for?

You might be able to create an array of functors that all derive from the same base class?
Other than that, I think you'd just have to cast it to the right function pointer when you use it.
 
Hi,

The array of pointers to functions would be be a library of functions that would be called using a execute command. So there would be a add_fn to add functions to a list and a execute_fn, the args are popped from the stack. The stack is an object containing the args. Since you can overload functions, then can do you overload a function pointer array? Or is there a different way to do this in C++?

Thanks, Rick.

sample code
-----------------------------------------
execute_fn(fn, num_args, top, Obj stk);

execute_fn(int (*fn)(), int args, int t, stk){
{
if (fn == NULL) abort();
switch (args) {
case 0:
(*fn)();
break;
case 1:
(*fn)(stk[t]);
break;
case 2:
(*fn)(stk[t], stk[t+1]);
break;
case 3:
(*fn)(stk[t], stk[t+1], stk[t+2]);
break;
default:
print "error";
break;
}
}
 
Are the functions that you want to add to the list written by you, or are they any function like printf()...?
What kind of limitations will the functions have on parameters & return type?
 
Hi,

The functions are within the program, not system ones like printf. The array of function pointers would have void return types, parameters would be either void to 1-5 class objects or structs, it could be objects if that's easier. The function pointer would be passed to the execute_fn function as above with the number of parameter args.

list of function types
----------------------
void fn();
void fn(Obj);
void fn(Obj,...);

Thanks, Rick
 
Damn... Hmmm... That's a tough one.
I was thinking you could do something like:
Code:
typedef void (*void_fn)();
typedef void (*void_fn_Obj)( Obj );
typedef void (*void_fn_Obj_int)( Obj, int );

class Functor
{
public:
   Functor()
   : m_void_fn( NULL ),
     m_void_fn_Obj( NULL ),
     m_void_fn_Obj_int( NULL )
   {}

   void operator()()
   {
      if ( m_void_fn != NULL )
         (*m_void_fn)();
   }

   void operator()( Obj p1);
   {
      if ( m_void_fn_Obj != NULL )
         (*m_void_fn_Obj)( p1 );
   }

   void operator()( Obj p1, int p2 );
   {
      if ( m_void_fn_Obj_int != NULL )
         (*m_void_fn_Obj_int)( p1, p2);
   }

   void SetFunc( void_fn  pFunc )
   { m_void_fn = pFunc; }

   void SetFunc( void_fn_Obj  pFunc )
   { m_void_fn_Obj = pFunc; }

   void_SetFunc( void_fn_Obj_int  pFunc )
   { m_void_fn_Obj_int = pFunc; }

private:
   void_fn          m_void_fn;
   void_fn_Obj      m_void_fn_Obj;
   void_fn_Obj_int  m_void_fn_Obj_int;
};
But now that I think about it a little more, I'm not sure if that would work the way you need either.

I guess you could always try it and find out.
It'll just get really big and messy if there's a lot of different types of function pointers to pass.

You might also want to create an empty base class and have one derived functor for each type of function pointer, then use a dynamic_cast when you pull it out of the array...
 
You know, this might even work better:
Code:
class FuncContainerBase
{
};

template <class T>
class FuncContainer  :  public FuncContainerBase
{
public:
   FuncContainer( T  pFunc )
   : m_FuncPtr( pFunc )
   {}

   T GetFunc() const
   { return m_FuncPtr; }

private:
   T  m_FuncPtr;
};
This way you can just call GetFunc() to get the raw function pointer and do whatever you want with it, and you won't need to write hundreds of functor classes.
 
Hi,

How would I use this GetFunc pointer? Please give me a example, I'm not familar with this type of function?

Thanks,

Rick.
 
Hi,

How would I use the code that cpjust shows in the previous reply? I would like an example.

cpjust writes...
This way you can just call GetFunc() to get the raw function pointer and do whatever you want with it, and you won't need to write hundreds of functor classes.

Thanks,

Rick.
 
I quoted this statement of yours in my post: "This would be for building a dictionary of function calls." I was asking you to elaborate on that statement. Specifically, how do you intend to use this data structure?
 
I can infer from your description that you want to do something like this:
Code:
// A: Put functions on the stack

while (!s.empty()) {
    // B: call the function on top of the stack.
    s.pop()
}

But do you need to bind the parameters to the function at the time they are registered to be called (A), or at the time of the call itself (B)?
 
You'd use it like this:
Code:
vector<FuncContainerBase&> funcPtrArray;
...
typedef void (*void_fn)();
typedef void (*void_fn_Obj)( Obj );
typedef void (*void_fn_Obj_int)( Obj, int );
...
FuncContainer<void_fn> aaa( &SomeFunction );
FuncContainer<void_fn_Obj> bbb( &SomeOtherFunction );
FuncContainer<void_fn_Obj_int> ccc( &YetAnotherFunction );

funcPtrArray.push_back( aaa );
funcPtrArray.push_back( bbb );
funcPtrArray.push_back( ccc );
You'd have to be careful when declaring your vector though since
Code:
vector<FuncContainerBase> funcPtrArray;
Will result in slicing. You'll have to use a reference or pointer to FuncContainerBase.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top