how to pass a function pointer

let say i have
void func1(vars)
void func3(vars)
void fun4(var);
void func2(UNKNOWN)
i call fun1 which determine which of func3 and func4 should be run from func2.  it should be done when application start.
i would like to tell func2 which one to run, I don't want to use variables and deciph then in func2. i would like to send the function pointer to func2 so it runs it.
can someone show me Please.
shisuckAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ZoppoConnect With a Mentor Commented:
Hi shisuck,

mabe somehow like this:

typedef void (*FUNC_PTR)( int, int );

void func2( FUNC_PTR pfunc )
{
 (pfunc)( 2, 3 );
}

void func3( int, int )
{
 cout << "Func3 called!" << endl;
}

void func4( int, int )
{
 cout << "Func4 called!" << endl;
}

void func1( int x )
{
 if ( x )
  func2( &func3 );    
 else
  func2( &func4 );
}

void main()
{
 func1( 0 );
 func1( 1 );
}

-----------------------------------------------
Ouput:
Func4 called!
Func3 called!
-----------------------------------------------

BTW, this only works when func3 and func4 are declare with same return value and same
arguments...

hope that helps,

ZOPPO
0
 
AxterCommented:
>>i would like to tell func2 which one to run, I don't
>>want to use variables and deciph then in func2.
>>i would like to send the function pointer to func2 so it
>>runs it.
If you have functions that have different variable type arguments, then you need to use a void* pointer.

That's the only way around this that I know of.

void func1(void*)
void func3(void*)
void fun4(void*)
void func2(void*)

Then use casting to pass the specific variable type back and forth.
0
 
ZoppoCommented:
Yes, I agree, but then you'll have to know in func2 how to cast the void pointer to
a function pointer, so you don't win anything...
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
AxterCommented:
>>so you don't win anything...
func2 should already know what type to cast to.  I don't think you understand what I'm suggesting.

func1(char *data);
func2(int *data);
func3(char *data);
func4(char *data);

I believe the questioner is asking if he/she has the the above functions, how can you make a function pointer that can apply to all four functions.  You can't.  Therefore if you have this type of requirement, the best you can do is make them all void, and then cast the types through.


0
 
ZoppoCommented:
>I don't think you understand what I'm suggesting.
Yep ... I understood you sugested to pass the function pointer as void*.
0
 
shisuckAuthor Commented:
okay let me make it easier.  the functions passed to func2 have a void return and both are passed an integer.
all functions are member functions of a class.
I understood what Zopo said, but not you Axter.  explain please
0
 
ZoppoCommented:
Here's what Axter meant:

I.e. if func3 and func4 should work with different argument types you can do it somehow
like this:

// func3() interprets passed pData as char*
// func4() interprets passed pData as int
typedef void (*FUNC_PTR)( void* );

void func2( FUNC_PTR pfunc, void* pData )
{
 (pfunc)( pData );
}

void func3( void* pData )
{
 cout << "String is: " << (char*)pData << endl;
}

void func4( void* pData )
{
 cout << "Len is: " << (int)pData << endl;
}

void func1( int x, char* t )
{
 if ( x )
  func2( &func3, (void*)t );    
 else
  func2( &func4, (void*)strlen( t ) );
}

void main()
{
 char* t = "Hello, world!";
 func1( 0, t );
 func1( 1, t );
}


Is this what you meant, Axter?

I hope so,

ZOPPO
0
 
AxterCommented:
>>Is this what you meant, Axter?
No.  That's not what I mean.
I don't have time to post an example right now.  I'll post later.  
0
 
jasonclarkeCommented:
> all functions are member functions of a class

this changes everything.  Member function pointers and normal function pointers are definitely not the same thing.

Are they all member functions of the same class?

How do you know the object instance that they are to be called on?
0
 
shisuckAuthor Commented:
yes, they are all member functions of the same class. actually they are private functions except for func1.  actually.  func1 determines which clean up function to pass to the class thread.
0
 
ZoppoCommented:
For a class same could look like this:

class X
{
 typedef void (X::*FUNC_PTR)( void* );
 void func2( FUNC_PTR pfunc, void* pData );
 void func3( void* pData );
 void func4( void* pData );
public:
 void func1( int x, char* t );
};

void X::func2( FUNC_PTR pfunc, void* pData )
{
 (this->*pfunc)( pData );
}

void X::func3( void* pData )
{
 cout << "String is " << (char*)pData << endl;
}

void X::func4( void* pData )
{
 cout << "Len is " << (int)pData << endl;
}

void X::func1( int x, char* t )
{
 if ( x )
  func2( &X::func3, (void*)t );    
 else
  func2( &X::func4, (void*)strlen( t ) );
}

void main()
{
 X x;
 char* t = "Hello, world!";
 x.func1( 0, t );
 x.func1( 1, t );
}

ZOPPO
0
 
AxterCommented:
Example code:
#include <string>

typedef const void* (*FUNC_PTR)(void*);

class car
{
public:
     car(){strcpy(szObjID,"car");};
     char szObjID[32];
};

class truck
{
public:
     truck(){szObjID = "truck";};
     std::string szObjID;
};

class bus
{
public:
     bus(){strcpy(szObjID,"bus");};
     char szObjID[32];
};

class bike
{
public:
     bike(){szObjID = "bike";};
     std::string szObjID;
};

class ObjectHolder
{
public:
     ObjectHolder(){};
     const char * GetObjectName(int Index)
     {
          FUNC_PTR function[4] = {func1,func2,func3,func4};
          void* Objects[4] = {&mycar,&mytruck,&mybus,&mybike};
          return (char*)function[Index&3](Objects[Index&3]);
     }
private:
     car mycar;
     truck mytruck;
     bus mybus;
     bike mybike;
     static const void* func1( void* pData ){return ((car*)(pData))->szObjID;}
     static const void* func2( void* pData ){return ((truck*)(pData))->szObjID.c_str();}
     static const void* func3( void* pData ){return ((bus*)(pData))->szObjID;}
     static const void* func4( void* pData ){return ((bike*)(pData))->szObjID.c_str();;}
};

void main()
{
     ObjectHolder myobject;
     printf("Object 1 ID = %s\n",myobject.GetObjectName(0));
     printf("Object 2 ID = %s\n",myobject.GetObjectName(1));
     printf("Object 3 ID = %s\n",myobject.GetObjectName(2));
     printf("Object 4 ID = %s\n",myobject.GetObjectName(3));
     printf("end\n");
}
0
 
shisuckAuthor Commented:
sorry it took me a while, could not get oN :-).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.