problem mit vererbung



  • also, ich hab atm ein kleines problem, und zwar hab ich ne klasse die von 2 baseclasses abgeleitet werden muss,das problem ist nun,dass beide baseclasses vom selben interface abgeleitet sind, und ich somit dann folgende struktur hab:

    Interface
          /         \
        base        base2
          \         /
            derived
    

    nun, damit kommt der compiler leider nicht klar, wie muss ich Interface nun vererben, damit es keine probleme gibt?(hab mal irgendwas hier im forum von virtual gehört,aber die suche hat nichts gutes ausgespuckt).



  • Hallo,

    ich habe mal im C++ Draft nachgeschaut und folgendes dazu gefunden:

    3 A class shall not be specified as a direct base  class  of  a  derived
      class  more  than  once.  [Note: a class can be an indirect base class
      more than once and can be a direct and  an  indirect  base  class.   ]
      [Example:
              class X { /* ... */ };
              class Y : public X, public X { /* ... */ };  // ill-formed
              class L { public: int next;  /* ... */ };
              class A : public L { /* ... */ };
              class B : public L { /* ... */ };
              class C : public A, public B { void f(); /* ... */ };   // well-formed
              class D : public A, public L { void f(); /* ... */ };   // well-formed
       --end example]
    
    4 A  base  class  specifier  that  does not contain the keyword virtual,
      specifies a nonvirtual base class.  A base class specifier  that  con-
      tains  the  keyword virtual, specifies a virtual base class.  For each
      distinct occurrence of a nonvirtual base class in the class lattice of
      the most derived class, the most derived object (_intro.object_) shall
      contain a corresponding distinct base class subobject  of  that  type.
      For  each  distinct  base  class  that  is specified virtual, the most
      derived object shall contain a single base  class  subobject  of  that
      type.   [Example:  for an object of class type C, each distinct occur-
      rence of a (non-virtual) base class L in the class lattice of C corre-
      sponds  one-to-one  with  a  distinct L subobject within the object of
      type C.  Given the class C defined above, an object of  class  C  will
      have two sub-objects of class L as shown below.
                                   L          L
                                   |           |
                                   |           |
                                   A          B
    
                                        C
    
      In  such lattices, explicit qualification can be used to specify which
      subobject is meant.  The body of function C::f could refer to the mem-
      ber next of each L subobject:
              void C::f() { A::next = B::next; }   // well-formed
      Without  the A:: or B:: qualifiers, the definition of C::f above would
      be ill-formed because of ambiguity (_class.member.lookup_).
    
    5 For another example,
              class V { /* ... */ };
              class A : virtual public V { /* ... */ };
              class B : virtual public V { /* ... */ };
              class C : public A, public B { /* ... */ };
      for an object c of class type C, a  single  subobject  of  type  V  is
      shared by every base subobject of c that is declared to have a virtual
      base class of type V.  Given the class C defined above, an  object  of
      class C will have one subobject of class V, as shown below.
    
                                        V
    
                                   A          B
    
                                        C
    

    Denke das ist genau das, was du gesucht hast. Ich selbst hab sowas bis jetzt
    noch nicht gemacht, deswegen kann ich dir hier nur en c&p ausm Draft statt
    einer eigenen Erklaerung bieten, hoffe aber, dass es dir weiterhilft.

    mfg
    v R



  • ja sowas habe ich letztens mal gemacht hier ist der code hoffe er kann helfen

    class MBase {
    public:
        virtual char* vf() const = 0;
        virtual ~MBase() {}
    };
    
    class D1 : virtual public MBase {
    public:
        char* vf() const { return "D1"; }
    };
    
    class D2 : virtual public MBase {
    public:
        char* vf() const { return "D2"; }
    };
    
    class MI : public D1, public D2 {
    public:
        char* vf() const { return D1::vf();}// expliziter zugriff nötig
    };
    

    also wie du schon sagst virtual vererbt zumindest habe ich es so "gelernt"..



  • ok, die version von truebool hat fehlerlos geklappt, darf das in die FAQ?^^



  • Du hast in A und in B a() nicht definiert.

    mfg
    v R



  • kombiniere einfach virtual und public zu

    class B : public virtual V { /*...*/ };
    

    Aber mit dieser Art von Vererbung bekommst auch noch ganz andere Probleme.
    siehe auch http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.12



  • @v r
    auch wenn ich den code vorhin wieder rauseditiert hab:

    daran liegts nicht. A wurde nur als pointer verwendet, und dafür gelten die einschränkungen nicht. nur wenn ich ein A objekt haben will muss ich dafür a() definieren.

    ansonsten würde sowas:

    C c;
    A* a=&c;
    a->a();
    

    eh nur C::a() aufrufen

    @davie was ein glück, dass ich eh nur ein interface evrerben muss^^


Anmelden zum Antworten