home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,900 of 33,346   
   Jerry to All   
   Passing pointer to member objects   
   08 Mar 13 00:22:46   
   
   41d7577e   
   From: jerry.jeremiah@googlemail.com   
      
   Hello to all of you.  I have something I am sure you can help with.   
   Consider this:   
      
   struct rec   
   {   
     int  id;   
     char label[20];   
   }   
      
   rec array[26] = {rec( 1,"a"),rec( 2,"b")};   
      
   template   
   R get(R* begin,R* end,V R::* m,V v)   
   {   
       for(R* it=begin;it!=end;it++)   
         if((*it).*m == v)   
           return (*it);   
       return R();   
   }   
      
   int main()   
   {   
     std::vector r1 = get(array,array+size,&rec::id,2);   
     return 0;   
   }   
      
   Ok that works.  But I can't do it with the other member like this:   
      
   int main()   
   {   
     std::vector r1 = get(array,array+size,&rec::label,"b");   
     return 0;   
   }   
      
   And the reason I can't is that get would have to be defined:   
      
   template   
   R get(R* begin,R* end,char R::* m[20],const char * v)   
   {   
   ...   
   }   
      
   Well I could but how many functions do I need since the size of the   
   array must be specified in the function signature?   
      
   Is there any way to generically define this function so I can pass any   
   structure member including char arrays of arbitrary length?   
      
   Thanks for the advice.  Also note that this compiler is over 10 years   
   old and conforms to C++98 but can't compile any recent Boost library.   
      
   Jerry   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca