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,874 of 33,346    |
|    Thomas Richter to All    |
|    Re: Virtual base class and friend    |
|    28 Feb 13 04:08:39    |
   
   From: thor@math.tu-berlin.de   
      
   Am 28.02.2013 09:02, schrieb omveer:   
   > Hi,   
   >   
   > I'm trying below scenario:   
   > class xx   
   > {   
   > xx(){}   
   > friend class yy;   
   > };   
   >   
   > class yy: virtual public xx   
   > {   
   > public:   
   > yy(){}   
   > };   
   >   
   > class zz:public yy   
   > {   
   > public:   
   > };   
   >   
   > Q1: It doesn't work with ZZ obj; Please help me understanding it.   
      
      
   Because, with the virtual in place, the every derived class (and thus   
   also zz in your code) and not only the next derived class needs to   
   construct the virtual bases (xx), and it cannot do that because the xx   
   constructor is private in zz. Without the virtual in place, the yy   
   constructor is responsible for constructing the xx object, and it can   
   do that because yy is a friend of xx.   
      
   Hence, with virtual in place, zz and yy are responsible for   
   constructing xx. Without it, only yy is.   
      
   So basically, what you have to understand is:   
      
   *) "friend" is not transitive. Just because a is a friend of b, and c   
   derives from a, doesn't make c a friend of b.   
      
   *) Construction of virtual base objects. These happen *not* in the   
   immediate superclasses, as it happens with regular inheritance, but in   
   every derived class.   
      
   Why is this so? Think about which problem virtual inheritance is   
   supposed to solve. (Hint, hint! The diamond shape, Hint, Hint!)   
      
   Greetings,   
    Thomas   
      
      
   --   
    [ 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