g++ macro help needed

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

g++ macro help needed

Postby fpga » Fri Aug 18, 2006 11:27 am

/*
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();
}
fpga
 
Posts: 5
Joined: Fri Aug 18, 2006 11:19 am

RE: g++ macro help needed

Postby Nigel » Fri Aug 18, 2006 12:10 pm

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: Select all
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.
User avatar
Nigel
LXF regular
 
Posts: 1141
Joined: Fri Apr 08, 2005 8:03 pm
Location: Gloucestershire, UK

RE: g++ macro help needed

Postby fpga » Fri Aug 18, 2006 12:57 pm

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
fpga
 
Posts: 5
Joined: Fri Aug 18, 2006 11:19 am

RE: g++ macro help needed

Postby Nigel » Fri Aug 18, 2006 2:22 pm

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.
User avatar
Nigel
LXF regular
 
Posts: 1141
Joined: Fri Apr 08, 2005 8:03 pm
Location: Gloucestershire, UK

RE: g++ macro help needed

Postby fpga » Fri Aug 18, 2006 3:23 pm

Thanks for the explanation.
I'll take your advice and try another approach.
Thanks
fpga
 
Posts: 5
Joined: Fri Aug 18, 2006 11:19 am


Return to Programming

Who is online

Users browsing this forum: Yahoo [Bot] and 0 guests