Please support our C++ advertiser: Programming Forums
![]() |
•
•
Join Date: Jun 2007
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
Hi All,
I am trying to implement the concept of function pointer in C++.
While compiling i got some errors which I couldnt figure out the reason .
So,Please let me know what changes should be done for resolving that error.
I am trying to execute one particular command function when the commandis pressed.Likewise, I have 100 commands and whenever the commands are pressed the corresponding function + arguements should be passed and the function pointer should execute the command.
Below are my code,
Please let me know where i should change the code.
Expecting some solutions for this code ASAP .
Thanks,
Sowmi
I am trying to implement the concept of function pointer in C++.
While compiling i got some errors which I couldnt figure out the reason .
So,Please let me know what changes should be done for resolving that error.
I am trying to execute one particular command function when the commandis pressed.Likewise, I have 100 commands and whenever the commands are pressed the corresponding function + arguements should be passed and the function pointer should execute the command.
Below are my code,
command.h
**********
#define FUNCPTR void *
#define MAX_NUM_COMMANDS 3
typedef struct
{
char command[LINELEN]; /* name of the command */
char parm1[LINELEN]; /*parameter 1 */
char parm2[LINELEN]; /*parameter 2 */
} ExeCmd_s;
class FunctionEntry
{
public:
char* commandString;
FUNCPTR pFun;
int SearchCommand(char* command);
};
FunctionEntry funArr[MAX_NUM_COMMANDS]=
{
{"aaa",(FUNCPTR)aaa},
{"bbb", (FUNCPTR)bbb},
{"ccc",(FUNCPTR)ccc}
};
enum commandName
{
aaa = 0,
bbb
};
command.c
**********
void void Execute ()
{
.........
.........
Execmd execmd;
FunctionEntry command;
int rc;
if(0 != SearchCommand(exeCmd.command))
{
command = FunctionEntry.SearchCommand(exeCmd.command);
switch((int)command.numOfArguements)
{
case Zero :
rc = ((command.pFun)();
break;
case one:
rc = ((command.pFun),exeCmd.parm1);
break;
}
}
else
{
printf("No match");
}
}
//Function to search and returns the index of the command
int FunctionEntry :: SearchCommand(char* command)
{
ExeCmd_s exeCmd; //already had a Structure where I am extracting the commandname..
int index = 0;
while(index < MAX_NUM_COMMANDS)
{
if(strcmp(exeCmd.command,funArr[index]) == 0)
{
return &funArr[index];
}
index++;
}
return NULL;
}Please let me know where i should change the code.
Expecting some solutions for this code ASAP .
Thanks,
Sowmi
Last edited by WaltP : Jun 27th, 2007 at 2:42 pm. Reason: Use the PREVIEW button to see if your post looks correct.
•
•
•
•
Hi All,
I am trying to implement the concept of function pointer in C++.
While compiling i got some errors which I couldnt figure out the reason .
So,Please let me know what changes should be done for resolving that error.
•
•
•
•
I am trying to execute one particular command function when the commandis pressed.Likewise, I have 100 commands and whenever the commands are pressed the corresponding function + arguements should be passed and the function pointer should execute the command.
Below is my code. Code is singular in programming.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
-- Pearl Williams
•
•
Join Date: Dec 2006
Location: india
Posts: 1,087
Reputation:
Rep Power: 10
Solved Threads: 163
•
•
•
•
...I have 100 commands and whenever the commands are pressed the corresponding function + arguements...
it is much more flexible to represent functions as objects . this would allow free functions, function objects and member functions to be treated polymorphically. also, since these are objects, they can support operations (eg.bind). in the following example, boost.function and boost.bind are used (from the boost libraries); however, both function and bind would be part of c++0x (they are in tr1).
cplusplus Syntax (Toggle Plain Text)
#include <iostream> #include <boost/function.hpp> #include <boost/bind.hpp> #include <map> using namespace std ; using namespace boost ; struct function_one // function object { void operator() ( const string& arg ) const { cout << "function_one( " << arg << " )\n" ; } }; void function_two( const string& arg ) // free function { cout << "function_two( " << arg << " )\n" ; } struct object { explicit object( int ii = 0 ) : i(0) {} int i ; void function_three( const string& arg ) // member function { cout << "object::function_three( " << arg << " )\n" ; ++i ; } }; int main() { typedef function< void ( const string& ) > function_t ; typedef pair< function_t, string > fn_with_arg_t ; map< string, fn_with_arg_t > function_map ; function_map[ "function object" ] = fn_with_arg_t( function_one(), "one" ); function_map[ "free function" ] = fn_with_arg_t( function_two, "two" ) ; object obj ; function_map[ "member function" ] = fn_with_arg_t( bind( &object::function_three, ref(obj), _1 ), "three" ); string key ; while( getline( cin, key ) ) { typedef map< string, fn_with_arg_t >::iterator iterator ; iterator ptr = function_map.find(key) ; if( ptr != function_map.end() ) ptr->second.first( ptr->second.second ) ; else cout << "function not found\n" ; } }
![]() |
Similar Threads
Other Threads in the C++ Forum
- Stack implementation using function pointers in C (C)
- Function pointers inside classes (C++)
- problems with function pointers (C)
Other Threads in the C++ Forum
- Previous Thread: Integer Stack Copy Function
- Next Thread: which process reads the cin
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode