Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

g++ macro help needed

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
fpga



Joined: Fri Aug 18, 2006 12:19 pm
Posts: 5

PostPosted: Fri Aug 18, 2006 12:27 pm    Post subject: g++ macro help needed Reply with quote

/*
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
View user's profile Send private message
Nigel
LXF regular


Joined: Fri Apr 08, 2005 9:03 pm
Posts: 1141
Location: Gloucestershire, UK

PostPosted: Fri Aug 18, 2006 1:10 pm    Post subject: RE: g++ macro help needed Reply with quote

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
View user's profile Send private message
fpga



Joined: Fri Aug 18, 2006 12:19 pm
Posts: 5

PostPosted: Fri Aug 18, 2006 1:57 pm    Post subject: RE: g++ macro help needed Reply with quote

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
View user's profile Send private message
Nigel
LXF regular


Joined: Fri Apr 08, 2005 9:03 pm
Posts: 1141
Location: Gloucestershire, UK

PostPosted: Fri Aug 18, 2006 3:22 pm    Post subject: RE: g++ macro help needed Reply with quote

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
View user's profile Send private message
fpga



Joined: Fri Aug 18, 2006 12:19 pm
Posts: 5

PostPosted: Fri Aug 18, 2006 4:23 pm    Post subject: RE: g++ macro help needed Reply with quote

Thanks for the explanation.
I'll take your advice and try another approach.
Thanks
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast