Function Pointers

Hi,

I have a class with about 30 member functions and I have an array of pointers to all of these functions. The problem is that to populate the array I have 30 lines of code assigning a pointer to each element.

Is there a better way? Like a loop that uses pointer arithmetic to move to the next function on each loop?

Thanks in advance
Can you post your code please? I don't understand what you're saying...
Sure!

I am struggling to explain this, but I am thinking that if all of the functions appear one after each other then maybe there's a way to get a pointer to the first one, increment some pointer to point to the next function, assign that function pointer to the array etc..

Seems like an idea in my head.. not sure if it's actually possible though

I have shown five of the functions that appear in the class LiveExports being assigned to my function pointer array.

1
2
3
4
5
LiveExportFuncPtr[0] = &LiveExports::LiveExportOne;
LiveExportFuncPtr[1] = &LiveExports::LiveExportTwo;
LiveExportFuncPtr[2] = &LiveExports::LiveExportThree;
LiveExportFuncPtr[3] = &LiveExports::LiveExportFour;
LiveExportFuncPtr[4] = &LiveExports::LiveExportFive;


So, I'm wondering if there is some sort of way to have a loop, and somehow populate the pointer array dynamically, instead of the one I've used above?

something like:
1
2
3
4
5
for (int x = 0; x < 5; x++)
{
    // assign function pointer
    // increment something somehow to move to the next function?
}

Nope .. There seems to be no way other then this.. thats my thinking .. please let me know if you find one.
You can't avoid typing the names of all the functions. There is no magic. The best you can get is this:

 
FnPtr funcs[] = { &LiveExports::LiveExportOne, &LiveExports::LiveExportTwo, ... };


which is one line of code in that it has only one semcolon, but it is no more or less maintainable
than your approach and not that much less typing either. (Nonetheless I'd prefer my approach
instead, though I'd probably use a boost::array<> instead of a C-style array.)
Ahhhh there goes my attempt at laziness :)

Thanks a lot for all replies
Hello dear all,


This is Lilly Michelle and i am newbie to this forum. I want become a c++ programmer. So i want to learn all those things in this forum. Thank you for providing information here.

______________________________________________________________________

Want to get-on Google's first page and loads of traffic to your website? Hire a SEO Specialist from Ocean Groups [url=http://oceangroups.org/] seo pecialist [/url]
ok well this is a little bit beyond me, but I recall one post (that was a little bit beyond me too) that gave me some ideas I think you might find useful... but check with the others on this forum about this idea before you waste your time.

could you declare all your functions in a header file just for your functions you want to be stored in your fp array... and then open the file for input (either b4 or after including it, I'm not sure which) and store the names of the function in a string or something and then assign the fp to the function, and use a while !eof to read until the file is done?

I'm guessing you probably can't, at least not in c++, but i sure bet you could in java. I'm just mentioning it here because I have a tendancy to think things more abstractly, which has a tendancy to be often more unneccessariy complex, so if it is possible, in the way I just mentioned, its more probably that this solution may have been overlooked by the more advanced programmers than myself. but if its not possible this way, i still bet that it is in java, judging by how c++ doesn't even have a command for breaking multiple control loops, just the single 'break;', and i'm told other languages have a format sucha s 'break(n)'.

anyways best of luck.
Topic archived. No new replies allowed.