Übereinstimmende Funktionen aus zwei unabhängigen Basisklassen



  • Hallo,

    ich habe drei Schnittstellen. Zwei unabhängige Schnittstellen (IFoo & IBar) und eine, die besagt, das beide anderen Schnittstellen enthalten sind (IFooBar).
    Jetzt habe ich eine Implementierung für IFoo bzw. IBar. Jetzt soll die Implementierung von IFooBar diese beiden verwenden und zusätzlich die IFooBar Schnittstelle haben.

    struct IFoo{
    	virtual void foo() = 0;
    };
    
    struct IBar{
    	virtual void bar() = 0;
    };
    
    struct IFooBar : public IFoo, public IBar {};
    
    struct Foo : public IFoo{
    	virtual void foo(){};
    };
    
    struct Bar : public IBar{
    	virtual void bar(){};
    };
    
    struct FooBar : public IFooBar, public Foo, public Bar {
    };
    
    int main(){
    	FooBar fooBar;
    }
    

    test.cpp: In function 'int main()':
    test.cpp:24:9: error: cannot declare variable 'fooBar' to be of abstract type 'FooBar'
    test.cpp:19:8: note: because the following virtual functions are pure within 'FooBar':
    test.cpp:6:15: note: virtual void IBar::bar()
    test.cpp:2:15: note: virtual void IFoo::foo()

    Wie löse ich es ohne jede Funktion zu delegieren?

    Gruß,
    Jörg



  • Mit virtueller Vererbung ist das möglich:

    struct IFoo{
        virtual void foo() = 0;
    };
    
    struct IBar{
        virtual void bar() = 0;
    };
    
    struct IFooBar : virtual public IFoo, virtual public IBar {};
    
    struct Foo : virtual public IFoo{
        virtual void foo(){};
    };
    
    struct Bar : virtual public IBar{
        virtual void bar(){};
    };
    
    struct FooBar : public IFooBar, public Foo, public Bar {
    };
    
    int main(){
        FooBar fooBar;
    }
    

    Sonst versucht FooBar, jeweils zwei Unterobjekte der Typen IFoo und IBar zu haben, von denen die durch IFooBar eingeführten abstrakt sind.



  • Danke



  • edit: gelöscht vom verdammt bösen volkard.


Anmelden zum Antworten