As far as I know, this isn't posssible. You could use a class with a memberfunction and then delcare an array of classes. Example:
#include <iostream>
using namespace std;
class foo
{
public:
int func(int a)
{
return a;
}
};
int main(void)
{
const int SIZE = 5;
foo bar[SIZE];
for (int i = 0; i < SIZE; i++)
cout << bar[i].func(i) << endl;
cin.get();
return 0;
}
Why would you want to do that anyway? Do the functions have the same implementation?
Perhaps you can clarify yourself with an example or code?
Niek
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
Actually, it is possible. You need to use token concatenation.
#define DEFINE_FUNC( n ) void func_ ## n()
You would use it in the normal way: DEFINE_FUNC( 12 );
The preprocessor would turn that into: void func_12();
While I can't imagine why you want to do this, there are, in fact, legitimate reasons to do token concatenation (which goes a long way into explaining why the preprocessor can do it at all).
Hope this helps.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
Actually, it is possible. You need to use token concatenation.
#define DEFINE_FUNC( n ) void func_ ## n()
How do you propose to do the precompiler looping?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Ah, I misunderstood the original post.
Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
> I heard something that boost's preprocessor library can do that.
> didn't get to try it out : http://boost.org/libs/preprocessor/doc/ref/repeat.html .
BOOST_PP_REPEAT gives a fast horizontal repetition from 0 to N-1
since you need a repetition starting at 1, using BOOST_PP_REPEAT_FROM_TO would be easier.
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
#include <iostream>
#define DEFUN( z, N, arg ) int BOOST_PP_CAT( arg, N )() \
{ return N * N ; }
#define DEFINE_FUNC(N) \
BOOST_PP_REPEAT_FROM_TO( 1, N, DEFUN, square_of_ )
DEFINE_FUNC(15)
int main()
{
std::cout << square_of_11() << '\n' ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287