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 31,956 of 33,346   
   Zeljko Vrba to yzghan@gmail.com   
   Re: How to avoid repeated run-time looku   
   25 Feb 12 13:51:45   
   
   8438ed98   
   From: mordor.nospam@fly.srk.fer.hr   
      
   On 2012-02-24, GH  wrote:   
   >   
   > But the   
   > runtime resolution for the virtual function for each element in the   
   > vector needs to be done only once at the first iteration of loop (1).   
   >   
   This implies that all members of the players vector have the same static   
   type.  Thus, you can do two things:   
      
   1. play() doesn't have to be virtual; just implement it in PlayerBase   
   2. Declare your start method as   
      
   template   
   void start(const vector& players) {   
     // call play   
     static_cast*>(*i)->play(this);   
   }   
      
   In this case, you don't declare play() in PlayerBase, but you DO   
   have to define it in Player.  Something as simple as   
   the following will work, and probably be optimized at compile-time:   
      
   template   
   class Player : .... {   
     void play(...) {   
       switch (CATEGORY) {   
   	case 1: // ..   
   	case 2: // ..   
   	//.. etc ..   
       }   
     }   
   };   
      
   [You can probably get more fancy with templates, typelists, etc., but   
   if play() is "simple", or there are few categories, I don't see the   
   point.]   
      
   ====   
      
   Your problem is kinda artificial.  If you know the static type of the   
   objects in the vector, why do you manipulate them through pointers to   
   base?  What is templating over CATEGORY supposed to achieve?  Why isn't   
   it a member field?   
      
      
   --   
         [ 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