|
-
Oct 6th, 2003, 04:19 AM
#1
Thread Starter
Fanatic Member
array of functions
Hello, just wondering how to have an array of functions.
You see, i have at the moment:
Code:
string ret;
switch (some_var)
case 1:
ret = some_function1(true); break;
case 2:
ret = some_function2(false); break;
....
but it would be nicer to make it:
Code:
ret = func_array[some_var](bool_input[some_var]);
now, the bool_input array is easy, just have = { true, false...}
but how do i get the array of funcitons to work?
sql_lall 
-
Oct 6th, 2003, 05:13 AM
#2
You can make an array of function pointers:
Code:
string func1(bool);
string func2(bool);
//...
typedef string (*bool_to_string_func)(bool);
bool_to_string_func func_array[] = {func1,func2};
-
Oct 7th, 2003, 04:12 AM
#3
Thread Starter
Fanatic Member
ok
thanks.
I guess there's no way to have such an array with functions that take various parameters. (i.e. some take one bool, other take two)
However, thanks for the code.
I guess to get different types of input, i can have the parameter of a vector<bool>, that way i can input as many as necessary...
sql_lall 
-
Oct 7th, 2003, 05:15 AM
#4
Frenzied Member
Sure there is.
Just make all of the functions that require fewer args use varargs - the ones that do not need all two or three parameters.
-
Oct 8th, 2003, 05:07 AM
#5
Thread Starter
Fanatic Member
umm...well
Yeah, unfortunately it didn't quite work.
How do you then call the funcitons?
func_array[1](true, true)??
and is there any way to use only one line, without the typedef?
Oh, one other small problem. I made a simple template function, outputs a value to file. This was a function in a class, and when i put the function in the class definition, it worked fine.
However, i prefer putting class function code in a .cpp file, so i tried putting in there:
template <class T> void myclass: rint(T input)
{}
but this wouldn't work. Any suggestions?
Note that in my class i had template <class T> print(T) - is this the correct prototype, or have i missed something??
Thanks
sql_lall 
-
Oct 8th, 2003, 10:01 AM
#6
template functions must always be put in header files (*)
And a vector<bool> or maybe a bool[] would be a better choice then a vararg function. Varargs are quite weird.
(*) you could use the export keyword or explicit instanciation, but you probably don't want to!
-
Oct 9th, 2003, 02:03 AM
#7
The export keyword is a non-standard extension. Supported AFAIK by VC++ and gcc, but not sure about that.
Explicit instantiation is useful only in some cases.
template <class T> void myclass::print(T input)
should be
template <class T> void myclass<T>::print(T input)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 9th, 2003, 02:04 AM
#8
For variable arguments, make each function take a
std::vector<boost::any>
.
This requires you to get the boost library from www.boost.org .
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 9th, 2003, 06:35 AM
#9
Thread Starter
Fanatic Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|