473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

array of functions?

I want to do something like this:


// declare a vector of type function
vector<(*foo)()> function_table;
//where
void foo()
{
do something
}
the above syntax isn't correct. How can I do it correctly?
thanks in advance for your answers.
Yous.
Jan 3 '06 #1
7 2579
Youssef Mesri wrote:
I want to do something like this:


// declare a vector of type function
vector<(*foo)()> function_table;
//where
void foo()
{
do something
}
the above syntax isn't correct. How can I do it correctly?
thanks in advance for your answers.
Yous.


#include <vector>
using namespace std;

void Foo1() {}
void Foo2() {}

int main()
{
typedef void (*pfn)();
vector<pfn> fns;
fns.push_back( Foo1 );
fns.push_back( Foo2 );
fns[0]();
return 0;
}

Cheers! --M

Jan 3 '06 #2
> vector<(*foo)()> function_table;

vector<return_type (*)(arg1_type, arg2_type)> function_table;

It's easier on everyone if you use a typedef, btw.

Luke

Jan 3 '06 #3

Youssef Mesri wrote:
I want to do something like this:
// declare a vector of type function
vector<(*foo)()> function_table;
//where
void foo()
{
do something
}
the above syntax isn't correct. How can I do it correctly?

You need a return type.

But I'd advise against doing it. Use instead a vector of pointers to a
base-class. Then invoke a virtual function of that base-class.

(Commonly this is the solution to calling a function based on a value
passed in at run time. Often the collection used is a map).

Jan 3 '06 #4
Earl Purple wrote:
Youssef Mesri wrote:
I want to do something like this:
// declare a vector of type function
vector<(*foo)()> function_table;
//where
void foo()
{
do something
}
the above syntax isn't correct. How can I do it correctly?


You need a return type.

But I'd advise against doing it. Use instead a vector of pointers to a
base-class. Then invoke a virtual function of that base-class.

(Commonly this is the solution to calling a function based on a value
passed in at run time. Often the collection used is a map).

I'm intersting to see an example prototype for this. can you please show
me an example.
thanks a lot
yous
Jan 3 '06 #5
On 2006-01-03 12:17:11 -0500, "Earl Purple" <ea********@gmail.com> said:

Youssef Mesri wrote:
I want to do something like this:
// declare a vector of type function
vector<(*foo)()> function_table;
//where
void foo()
{
do something
}
the above syntax isn't correct. How can I do it correctly?

You need a return type.

But I'd advise against doing it. Use instead a vector of pointers to a
base-class. Then invoke a virtual function of that base-class.


But then, you have to worry about managing the lifetime of the
pointed-to objects. If one were to simply store pointers to functions
in the container, then that issue goes away.
--
Clark S. Cox, III
cl*******@gmail.com

Jan 3 '06 #6
I'm intersting to see an example prototype for this. can you please show
me an example.
thanks a lot
yous


A very simple one using a fixed size array of 256.

typedef std::basic_string< unsigned char > ustring;

class MessageProcessor
{
public:
virtual void processMessage( const ustring & msg ) = 0;
virtual ~MessageProcessor() {}
};

class MessageProcessorTable
{
private:
enum { tableSize=256 };

MessageProcessor* itsTable[ tableSize ];
public:
MessageProcessorTable()
{
std::fill_n( &itsTable[0], tableSize, 0 );
}

void addProcessor( unsigned char idx, MessageProcessor * pmp )
{
itsTable[ idx ] = pmp;
}
bool processMessage( const ustring & msg ) const
{
if ( !msg.empty() )
{
MessageProcessor * pmp = itsTable[ msg[0] ];
if ( pmp )
{
pmp->processMessage( msg );
return true;
}
}
return false;
}
};

Obviously you then create an instance of the table, instances of each
message and add them to the table, then process your messages as they
come in. And you can introduce error handling features where a message
could not be processed - log the error, throw an exception or however
you want to handle it.

And note this is an illustration.In reality you might separate the
definition and implementation of the classes into .h and .cpp files.

Jan 4 '06 #7

Clark S. Cox III wrote:
On 2006-01-03 12:17:11 -0500, "Earl Purple" <ea********@gmail.com> said:
But then, you have to worry about managing the lifetime of the
pointed-to objects. If one were to simply store pointers to functions
in the container, then that issue goes away.


3 options:

1. Use smart pointers.
2. Use global objects. After all, these objects are only really
wrappers for functions so unless they hold state (thus potentially
being non-thread-safe to have global ones) it isn't really any
different. Certainly it can be no worse than using functions.
3. As I've put them (see my other post) into a table then the table can
delete them in its destructor. (Table would then also need overloaded
copy-constructor and assignment).

Most of the time I would go for option 2 in this situation. Often these
are actually objects that are dynamically loaded from a library. Note
that if you are programming using POSIX , this is the preferred way as
the result of dlsym is a void* pointer, i.e. a pointer to an object not
to a function.

(I might go for option 1 sometimes. Would rarely opt for 3 unless 2
wasn't an option and I needed higher performance than 1).

Jan 4 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
4
by: Mark Hannon | last post by:
I am trying to initialize an array only once so it can be seen & used by any functions that need it. As I understand it, if a variable is declared by itself outside of any functions, its scope is...
32
by: Carson | last post by:
Hi , Is there a very efficient way to set a double array to 0 ? (I have tried memset, but the result doesn't look correct.) Carson
5
by: Steve | last post by:
Can anyone tell me if I can have an array of functions that take a variable number of parameters? If it is possible I'd like to know how to declare the array and the functions as its elements. I am...
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
9
by: JoeC | last post by:
I am crating a new version of my map game and my map will be a 2d array. I had problems trying to create a 2d array dynamically, in fact C++ won't let me do it. My question is how to create the...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
3
by: David K in San Jose | last post by:
I'm using managed (CLR) C++ in VS2005 to create a Windows app that contains a form named "MyForm". In the code for that form I'm trying to invoke some static functions by using an array of function...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.