Solved

how to pass a function pointer

Posted on 2001-06-20
13
319 Views
Last Modified: 2010-04-02
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.
0
Comment
Question by:shisuck
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 50 total points
ID: 6210172
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
 
LVL 30

Expert Comment

by:Axter
ID: 6210210
>>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
 
LVL 31

Expert Comment

by:Zoppo
ID: 6210289
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 30

Expert Comment

by:Axter
ID: 6210481
>>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
 
LVL 31

Expert Comment

by:Zoppo
ID: 6210756
>I don't think you understand what I'm suggesting.
Yep ... I understood you sugested to pass the function pointer as void*.
0
 

Author Comment

by:shisuck
ID: 6210772
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
 
LVL 31

Expert Comment

by:Zoppo
ID: 6210825
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
 
LVL 30

Expert Comment

by:Axter
ID: 6210862
>>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
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6212303
> 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
 

Author Comment

by:shisuck
ID: 6212663
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
 
LVL 31

Expert Comment

by:Zoppo
ID: 6213289
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
 
LVL 30

Expert Comment

by:Axter
ID: 6213856
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
 

Author Comment

by:shisuck
ID: 6310662
sorry it took me a while, could not get oN :-).
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now