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,451 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All   
   Re: Covariant return types and interdepe   
   05 Sep 11 15:22:19   
   
   0b60190a   
   From: daniel.kruegler@googlemail.com   
      
   Am 31.08.2011 14:52, schrieb Javier:   
   > I'm having a problem trying to get two classes that refer to each   
   > other take a covariant return type.  While this is occurring in the   
   > Visual C++ 2010 compiler (and I'm posting a modified version here of   
   > my original post in the C++ forums at MS), I have a more general   
   > question, namely if there is a way for a compiler to pass what would   
   > be, in my opinion, valid code for covariant return types, or if I'm at   
   > the mercy of the compiler vendor :-)   
      
   [..]   
      
   To solve your problem practically, you could consider to take advantage   
   of two helper class templates as shown below. This approach uses the NVI   
   (non-virtual interface) idiom and has other advantages, too (E.g. it is   
   easy to simulate covariant return types with smart pointers):   
      
   #include    
      
   struct base2;   
      
   struct base1 {   
   protected:   
     virtual base2* doGetBase2() const = 0;   
   };   
      
   struct base2 {   
   protected:   
     virtual base1* doGetBase1() const = 0;   
   };   
      
   template   
   struct base1_nvi : public base1 {   
   public:   
     Derived* getBase2() const {   
       static_assert(std::is_base_of::value, "Derived must be a   
   sub-class of base2");   
       return static_cast(this->doGetBase2());   
     }   
   };   
      
   template   
   struct base2_nvi : public base2 {   
   public:   
     Derived* getBase1() const {   
       static_assert(std::is_base_of::value, "Derived must be a   
   sub-class of base1");   
       return static_cast(this->doGetBase1());   
     }   
   };   
      
   struct derived1;   
      
   struct derived2 : public base2_nvi {   
   protected:   
     virtual base1* doGetBase1() const override { return ...; }   
   };   
      
   struct derived1 : public base1_nvi {   
   protected:   
     virtual base2* doGetBase2() const override { return ...; }   
   };   
      
   Even though this example uses some C++11 language tools, these can be   
   easily replaced by corresponding boost tools in a complete C++03   
   conforming code.   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
   --   
         [ 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