| | |
macro that defines functions
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
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
C++ Syntax (Toggle Plain Text)
#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; }
Perhaps you can clarify yourself with an example or code?
Niek
Last edited by niek_e; Feb 19th, 2008 at 6:04 am. Reason: Example code
Actually, it is possible. You need to use token concatenation.
You would use it in the normal way:
The preprocessor would turn that into:
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.
#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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Jan 2008
Posts: 119
Reputation:
Solved Threads: 10
•
•
•
•
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.
DEFINE_FUNC (12)
the preprocessor should replace with 12 function declarations named func_1 func_2 ... func_12
so as Dave said, the looping is the problem
anyway as I said, I managed to carry out my stuff without using this, but if someone can figure out how to do the macro thingie I would be thankfull. 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.
cheers
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> 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.
> 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.
c++ Syntax (Toggle Plain Text)
#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' ; }
Last edited by vijayan121; Feb 20th, 2008 at 8:53 am.
![]() |
Similar Threads
- Linker Error (C++)
- Standard Code Indentation (C++)
- WinSock problems. (C++)
- airplane seating prices (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ sequential file / array
- Next Thread: Need help (newbie)
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






