Solved

how to pass a function pointer

Posted on 2001-06-20
13
314 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 30

Accepted Solution

by:
Zoppo earned 50 total points
Comment Utility
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
Comment Utility
>>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 30

Expert Comment

by:Zoppo
Comment Utility
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
 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>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 30

Expert Comment

by:Zoppo
Comment Utility
>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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
>>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
Comment Utility
> 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
Comment Utility
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 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
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
Comment Utility
sorry it took me a while, could not get oN :-).
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 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

10 Experts available now in Live!

Get 1:1 Help Now