| View previous topic :: View next topic |
| Author |
Message |
fpga
Joined: Fri Aug 18, 2006 12:19 pm Posts: 5
|
Posted: Fri Aug 18, 2006 12:27 pm Post subject: g++ macro help needed |
|
|
/*
I'm trying to assign an ordinary function to a ptr in an object.
If I pass the objects name to the macro 'RULES_TO_OBJECTS'
explicitly, then the code works fine.
I'd like to pass an object pointer to the macro instead, though,
but appear to be falling foul of a lack of macro expansion.
Has anyone got a solution please?
Thanks in anticipation.
*/
#include <iostream>
using namespace std;
#define RULES_TO_OBJECTS(nm) {nm.pRule = nm ## Rule;}
class Myclass {
public:
char * nam;
int (*pRule)();
};
//Rule function created externally
int objRule() {cout << 4 <<endl; return 4;}
int main() {
Myclass obj;
//=============================
// i'd like to replace this line
RULES_TO_OBJECTS(obj);
//=============================
//=============================
//with this
// Myclass * pObj = &obj;
// SOME_MACRO(pObj)
//ie using a ptr to an object & get the same result
//=============================
obj.pRule();
} |
|
| Back to top |
|
 |
Nigel LXF regular

Joined: Fri Apr 08, 2005 9:03 pm Posts: 1141 Location: Gloucestershire, UK
|
Posted: Fri Aug 18, 2006 1:10 pm Post subject: RE: g++ macro help needed |
|
|
I think the problem is that your macro is set up to receive the address of a structure. You want to pass the address of a pointer to the structure.
Try this :
| Code: | Myclass *pObj = &obj;
RULES_TO_OBJECTS (*obj); |
or possibly redefine your macro to expect a pointer to a structure and not a structure. _________________ Hope this helps,
Nigel. |
|
| Back to top |
|
 |
fpga
Joined: Fri Aug 18, 2006 12:19 pm Posts: 5
|
Posted: Fri Aug 18, 2006 1:57 pm Post subject: RE: g++ macro help needed |
|
|
Nigel
Thanks for your quick response.
The sticking point seems to be the ## concatenation operation
ie I can't convert anything other than 'obj' (supplied as an argument) so that
the ## statement ends up as :
'obj ## Rule'
ie the compiler always sees the first token as something other than obj.
This seems to be the case even when what is converted couts as obj |
|
| Back to top |
|
 |
Nigel LXF regular

Joined: Fri Apr 08, 2005 9:03 pm Posts: 1141 Location: Gloucestershire, UK
|
Posted: Fri Aug 18, 2006 3:22 pm Post subject: RE: g++ macro help needed |
|
|
Hmm... trouble is that macros (as I understand them) are resolved at compile time.
As I read it, you want to create a function name from an object name by adding "Rules" onto the end of it. That won't work unless you have the name of the object at compile time - which you do when you just pass obj, but you don't when you pass a pointer to obj.
So I think you're going to have to come at this one from another angle. _________________ Hope this helps,
Nigel. |
|
| Back to top |
|
 |
fpga
Joined: Fri Aug 18, 2006 12:19 pm Posts: 5
|
Posted: Fri Aug 18, 2006 4:23 pm Post subject: RE: g++ macro help needed |
|
|
Thanks for the explanation.
I'll take your advice and try another approach.
Thanks |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|