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,074 of 33,346   
   Ivan Godard to All   
   Ladder inheritance   
   30 Mar 12 23:54:42   
   
   From: igodard@pacbell.net   
      
   Given:   
    struct A0 {};   
    struct B0 : A0 {};   
    struct C0 : B0 {};   
   which are not to be changed, and needing the effect of:   
    struct A1 : A0 {}   
    struct B1 : B0, A1 {};   
    struct C1 : C0, B1 {};   
   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   
      
   The obvious approach is:   
      
    struct A0 { int a0; };   
    struct B0 : A0 { int b0; };   
    struct C0 : B0 { int c0; };   
    struct A1 : virtual A0 { int a1; };   
    struct B1 : virtual B0, virtual A1 { int b1; };   
    struct C1 : virtual C0, virtual B1 { int c1; };   
   int main() {   
    C1 cx;   
    cx.a0 = cx.b0 = cx.c0 = cx.a1 = cx.b1 = cx.c1 = 0;   
    return 0;   
   }   
      
   but that gets you (g++ 4.6.3):   
      
   foo.cc: In function âint main()â:   
   foo.cc:10:9: error: request for member âa0â is ambiguous   
   foo.cc:2:17: error: candidates are: int A::a0   
   foo.cc:2:17: error:                 int A::a0   
   foo.cc:2:17: error:                 int A::a0   
   foo.cc:10:17: error: request for member âb0â is ambiguous   
   foo.cc:3:22: error: candidates are: int B::b0   
   foo.cc:3:22: error:                 int B::b0   
   (Comeau gives a better diagnostic but reports the same errors).   
      
   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?   
      
      
   --   
         [ 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