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,689 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to Lorenzo Caminiti   
   Re: Covariant virtual function result ty   
   24 Nov 11 00:49:29   
   
   8807a3ed   
   From: daniel.kruegler@googlemail.com   
      
   On 2011-11-24 01:40, Lorenzo Caminiti wrote:   
   >   
   > When invoking a covariant virtual function, the derived function body   
   > is executed but the result type of the base function is returned. Is   
   > there any way to get C++ to return the result type of the derived   
   > function instead?   
      
   How should that work? You seem to ask a question based a dynamic runtime   
   behaviour and expect a compile-time answer. This looks like a   
   contradiction to me.   
      
   > For example:   
   >   
   > #include   
   >   
   > struct rx {};   
   > struct ry : rx {};   
   >   
   > struct x {   
   >     virtual rx* f() {   
   >         std::cout<<  "rx* f()"<<  std::endl;   
   >         return&rx_;   
   >     }   
   >     rx rx_;   
   > };   
   >   
   > struct y : x {   
   >     ry* f() {   
   >         std::cout<<  "ry* f()"<<  std::endl;   
   >         return&ry_;   
   >     }   
   >     ry ry_;   
   > };   
   >   
   > int main ( ) {   
   >     y yy;   
   >     y* yp =&yy;   
   >     x* xp =&yy;   
   >   
   >     ry* ryp = yp->f(); // calls y::f   
   >     rx* rxp = xp->f(); // calls y::f but returns rx* instead of ry*   
   >   
   > //    ry* nope = xp->f(); // error: calls y::f but returns rx* instead   
   > of ry*   
   >   
   >     return 0;   
   > }   
   >   
   > Here both yp->f() and xp->f() actually execute the body of the derived   
   > class y (via dynamic binding). However, the compiler doesn't know that   
   > until run-time so yp->f() returns a ry* but xp->f() returns a rx*   
   > (even if y::f() which returns ry* is executed in both cases). Because   
   > of that the declaration of nope doesn't compile because it uses ry*   
   > instead of rx*.   
      
   So why are you trying to go through x*, if you actually intend to act on   
   an y*?   
      
   > Is there any way to call f() via xp and have the compiler recognize   
   > that y::f will be executed and therefore that ry* will actually be the   
   > result type? (C++11 tricks (if there's any that might help in this   
   > case) also allowed.)   
      
   C++11 tricks won't help you here - you have a design problem. From your   
   description it seems as if you can always rightly ask for an ry*,   
   because you argue that the rx-ry hierarchy is independent from the x-y   
   hierarchy. In this case you could change your design by adding a virtual   
   function to x, that gives a potential nullptr of ry*. This means, that   
   the x-y hierarchy is not interested in presenting a sub-class view on   
   x-y. E.g.   
      
   struct x {   
     virtual ry* f() { return nullptr; }   
   };   
      
   struct y : x {   
     virtual ry* f() { return &ry_; }   
     ry ry_;   
   };   
      
   Such design is useful, if we consider ry as an optional attribute of x.   
      
   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