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,075 of 33,346   
   Richard Damon to Ivan Godard   
   Re: Ladder inheritance   
   31 Mar 12 21:00:21   
   
   From: news.x.richarddamon@xoxy.net   
      
   On 3/31/12 2:54 AM, Ivan Godard wrote:   
   ...   
   > The goal here is that the X1 hierarchy should be topologically the same   
   > as the X0 hierarchy, but with a few added members at each level. The   
   > desired inheritance graph looks like a ladder:   
   > A0  <-  A1   
   >  ^      ^   
   > B0  <-  B1   
   >  ^       ^   
   > C0  <-  C1   
   ...   
   > Making all the bases in the X0 hierarchy virtual avoids the errors, but   
   > that's not an available choice in my use case, and in any case would   
   > hose any other derivations that did not want virtual inheritance from   
   > the X0 hierarchy.   
   >   
   > Essentially I am trying to derive a whole inheritance graph from another   
   > whole inheritance graph, rather than deriving a single class from   
   > another single class. I expected that deriving from a virtual base class   
   > would implicitly make all the bases of the virtual base be also virtual;   
   > apparently not.   
   >   
   > Is there any way to do this? If not, why was inheritance from a virtual   
   > base defined this way?   
   >   
      
   While in your case it might be nice to combine the single not-virtual   
   base with same class as a virtual base, in general you don't necessarily   
   have a SINGLE instance of the class as a non-virtual base, and   
   connecting a virtual base declaration to the single usage could yield   
   "surprising" results as the non-virtual derivation has an implication   
   that the base class is "yours alone", shared with those derived from   
   you, but not siblings of them. In your case B0 might be surprised at a1   
   "stealing" it a0 base.   
      
   One way to get something close to what you want is to have your *1 tree   
   not derive from the *0 tree but to contain and forward. Thus A1 has a   
   reference to an A0 in it, with an access function to get it, which is   
   overridden in B1, to make the return a B0& (and insures the *0 is at   
   least a B0), and so on. Each *1 class needs to declare any needed   
   functions for the corresponding *0 class, with the appropriate   
   forwarding. Note that one limitation of this is that a virtual function   
   in the *0 chain can not be overridden in the *1 chain (without help in   
   the *0 chain for a call back).   
      
   Another method, as you noted, is to have the *0 chain use virtual   
   inheritance to allow for all the inheritance diamonds you are creating.   
   This does allow all the paths to override virtual functions, which also   
   means that each level needs to handle any possible ambiguity in the   
   virtual functions.   
      
      
   --   
         [ 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