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,936 of 33,346   
   =?ISO-8859-1?Q?=D6=F6_Tiib?= to Seungbeom Kim   
   Re: Can I overload with unused arguments   
   16 Mar 13 16:57:58   
   
   From: ootiib@hot.ee   
      
   On Saturday, 16 March 2013 12:50:02 UTC+2, Seungbeom Kim  wrote:   
   > On 2013-03-14 23:08, Öö Tiib wrote:   
   > >   struct Washer {   
   > >      // washers wash ;-)   
   > >      void wash();   
   > >       // sometimes with soap   
   > >      void wash( Soap& );   
   > >   };   
   > >   
   > >   struct SoapEater {   
   > >      // will throw you with heavy things if you give no real soap   
   > >      void eat( std::unique_ptr );   
   > >   };   
   > >   
   > > Usage:   
   > >   
   > >      Washer w;   
   > >      SoapEater e;   
   > >      std::unique_ptr soap = searchForSoap(); // unsure if found   
   > >   
   > >      if (!soap)   
   > >      {   
   > >          w.wash();   // washer can wash with water only   
   > >          // leave eater alone   
   > >      }   
   > >      else   
   > >      {   
   > >          w.wash( *soap );   
   > >          e.eat( std::move(soap) );   
   > >      }   
   >   
   > It was not clear in the OP's description whether the presence of   
   > an entertainer was determined at run-time, but if the presence of   
   > soap is determined at run-time as in your example above, I would   
   > simply go for a pointer argument.   
      
   I meet more often situations when presence of something is on some   
   cases known compile time and on some cases is not. Even if initially   
   it was always not known it tends to change when software evolves.   
      
   >      void Washer::wash(Soap* soap)   
   >      {   
   >          if (soap == NULL) {   
   >              // wash without soap   
   >          }   
   >          else {   
   >              // wash with *soap   
   >          }   
   >      }   
      
   You have two functions in one so it can not be called "simpler". It may   
   be easily added as a third overload however:   
      
         void Washer::wash(Soap* soap)   
         {   
             if (soap == nullptr) {   
                 wash();   
             }   
             else {   
                 wash( *soap );   
             }   
         }   
      
   Works as well.   
      
      
   --   
         [ 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