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 

C++ macros

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



Joined: Fri Apr 08, 2005 4:20 pm
Posts: 83
Location: Cambridge, England

PostPosted: Mon Jun 27, 2005 11:34 am    Post subject: C++ macros Reply with quote

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



Joined: Wed Jun 29, 2005 9:20 pm
Posts: 1

PostPosted: Wed Jun 29, 2005 9:30 pm    Post subject: RE: C++ macros Reply with quote

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


Joined: Fri Apr 08, 2005 2:32 am
Posts: 1996
Location: Sydney, Australia

PostPosted: Wed Jun 29, 2005 11:16 pm    Post subject: RE: C++ macros Reply with quote

Thats a really good problem Smile

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 Smile


jm
Back to top
View user's profile Send private message
tedius



Joined: Fri Apr 08, 2005 4:20 pm
Posts: 83
Location: Cambridge, England

PostPosted: Thu Jun 30, 2005 2:18 pm    Post subject: Reply with quote

Thanks for the help guys.

I had sort of come to the conclusion that what I was trying was not going to work Sad

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. Smile

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 Sad
Back to top
View user's profile Send private message MSN Messenger
jjmac
LXF regular


Joined: Fri Apr 08, 2005 2:32 am
Posts: 1996
Location: Sydney, Australia

PostPosted: Fri Jul 01, 2005 11:55 am    Post subject: Reply with quote

>>
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 Sad
>>

Arrha, i see Smile

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 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