 |
Linux Format forums Help, discussion, magazine feedback and more
|
| View previous topic :: View next topic |
| Author |
Message |
tedius

Joined: Fri Apr 08, 2005 4:20 pm Posts: 83 Location: Cambridge, England
|
Posted: Mon Jun 27, 2005 11:34 am Post subject: C++ macros |
|
|
I'm trying to write a macro that will call a function to copy some values.
The extract below is only a small part of my program but I hope it will give you the idea of what I'm trying to do.
| Code: | #define COPY_ATT(dest, source, num) \
copyAtt((dest)->att_ ## num, \
source,
num);
std::string[3] list;
class object
{
std::string att_1;
std::string att_2;
std::string att_3;
};
copyAtt(std::string att,
std::string[] list,
int index)
{
att = list[num-1];
}
int main()
{
object *pObj new object;
for (i = 1; i <= 3; i++)
COPY_ATT(obj, list, i);
}
|
What is happening is that the macro is evaluating to (on the first loop)
| Code: | copyAtt(pObj->att_i,
list,
1);
|
Where I was hopping for
| Code: | copyAtt(pObj->att_1,
list,
1);
|
Is it at all posible to do what I want, and if so can some one tell me where I'm going wong.
Thanks |
|
| Back to top |
|
 |
vorian
Joined: Wed Jun 29, 2005 9:20 pm Posts: 1
|
Posted: Wed Jun 29, 2005 9:30 pm Post subject: RE: C++ macros |
|
|
Macros are evaluated by the preprocessor not the core compiler. You should treat it like string substitution.
After the preprocessing stage your code looks like:
for (i = 1; i <= 3; i++)
copyAtt((dest)->att_i, \
list,
i);
now the core compiler gets to work and seeks to find a att_i attribute (there is none ). |
|
| Back to top |
|
 |
jjmac LXF regular
Joined: Fri Apr 08, 2005 2:32 am Posts: 1996 Location: Sydney, Australia
|
Posted: Wed Jun 29, 2005 11:16 pm Post subject: RE: C++ macros |
|
|
Thats a really good problem
But, as suggested above ... the preprocessor can only reall do a compile-time substitution. And it will require a 'constant' to use in the substitution.
Where as, your looking for a run-time evaluation. I don't think the preprocessor can do that.
You would need to provide some trick to allow for a 'constant' to be passes, like the below.
| Code: |
for (int i = 1; i <= 3; i++){
switch (i){
case 1:
COPY_ATT(ob->att_1, i, i);
break;
case 2:
COPY_ATT(ob->att_2, i, i);
break;
}
}
|
That could get somewhat messy if you had a lot of strings to copy.
As a container ... why not use a 'vector' instead ?. Sounds like it would be perfect for what you see, to be doing there.
About all i know about macro substitions though, comes from here ...
http://gcc.gnu.org/onlinedocs/cpp/Macros.html
So i could well be missing something there
jm |
|
| Back to top |
|
 |
tedius

Joined: Fri Apr 08, 2005 4:20 pm Posts: 83 Location: Cambridge, England
|
Posted: Thu Jun 30, 2005 2:18 pm Post subject: |
|
|
Thanks for the help guys.
I had sort of come to the conclusion that what I was trying was not going to work
The solution that I am using is just to remove it from the loop and do one line for each copy. It works, but with 34 attributes to copy I was hoping for something more cleaver.
jjmac.
The reason I didn't use a vector is that the struct that I'm coping to is already define, and I don't have control over it  |
|
| Back to top |
|
 |
jjmac LXF regular
Joined: Fri Apr 08, 2005 2:32 am Posts: 1996 Location: Sydney, Australia
|
Posted: Fri Jul 01, 2005 11:55 am Post subject: |
|
|
>>
The reason I didn't use a vector is that the struct that I'm coping to is already define, and I don't have control over it
>>
Arrha, i see
The "switch" will work though, it's just that the preproccesor will need a const at compile-time, for the substitution.
jm _________________ http://counter.li.org
#313537
The FVWM wm -=- www.fvwm.org -=-
Somebody stole my air guitar, It happened just the other day,
But it's ok, 'cause i've got a spare ... |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|
|
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
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|