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,563 of 33,346   
   Christof Meerwald to All   
   covariant return types and std::unique_p   
   25 Sep 12 13:33:07   
   
   From: NOSPAM-seeMySig+uqk5+@usenet.cmeerw.org   
      
   Hi,   
      
   I was just wondering the other day what the preferred way of rewriting   
   the following code fragment (using covariant return types) in C++11   
   (using std::unique_ptr for the return types) would be:   
      
      struct A   
      {   
        virtual A *createAnother() = 0;   
        virtual void foo() = 0;   
      };   
      
      struct B : A   
      {   
        B *createAnother()   
        {   
          B *b = new B(*this);   
          b->doSomeStuff();   
          return b;   
        }   
      
        void foo();   
        void bar();   
      
      private:   
        void doSomeStuff();   
      };   
      
      int main()   
      {   
        B *b1 = new B();   
      
        B *b2 = b1->createAnother();   
        b2->foo();   
        b2->bar();   
      }   
      
      
   Obviously, just replacing the raw pointers with std::unique_ptr won't   
   work (without some additional casts) - but what's the cleanest way to   
   make it work (with the emphasis being on "cleanest")?   
      
      
   Christof   
      
   --   
   http://cmeerw.org                              sip:cmeerw at cmeerw.org   
   mailto:cmeerw at cmeerw.org                   xmpp:cmeerw at cmeerw.org   
      
      
         [ 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