 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Stephan Keil Guest
|
Posted: Thu May 06, 2004 9:16 am Post subject: Multiple Inheritance |
|
|
Hi,
folgendes Beispiel:
class A { public: virtual int f(); };
class B { public: virtual double f(); };
Gibt es eine Moeglichkeit von A und B gleichzeitig abzuleiten und gezielt
ein f() (oder beide) zu ueberschreiben?
Also z.B.
class AB : public A, public B { public: int f(); };
Der Compiler (MS VC.NET) motzt (zu recht , dass sich AB::f() von B::f()
nur im Rueckgabewert unterscheidet.
Gibt es eine spezielle Syntax oder ist das eine Luecke von C++?
Danke & Gruss, Stephan
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Markus Breuer Guest
|
Posted: Thu May 06, 2004 9:35 am Post subject: Re: Multiple Inheritance |
|
|
| Quote: | class A { public: virtual int f(); };
class B { public: virtual double f(); };
Gibt es eine Moeglichkeit von A und B gleichzeitig abzuleiten und gezielt
ein f() (oder beide) zu ueberschreiben?
Also z.B.
class AB : public A, public B { public: int f(); };
|
Hast du es einmal mit
class AB : public A, public B { public: int A::f(); };
probiert?
Gruß Maruks
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Stephan Keil Guest
|
Posted: Thu May 06, 2004 10:33 am Post subject: Re: Multiple Inheritance |
|
|
| Quote: | Hast du es einmal mit
class AB : public A, public B { public: int A::f(); };
probiert?
|
Dem Compiler gefaellt das nicht :-(
error C3240: 'f' : must be a non-overloaded pure virtual member function of
'A'
error C2838: 'f' : illegal qualified name in member declaration
An explicit override can only be used for a method that was first
introduced in an interface and whose override qualifier is an interface
error C2555: 'AB::f': overriding virtual function return type differs and
is not covariant from 'B::f'
Sollte das denn funktionieren?
Stephan
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Helmut Zeisel Guest
|
Posted: Thu May 06, 2004 10:59 am Post subject: Re: Multiple Inheritance |
|
|
Stephan Keil wrote:
| Quote: | Hast du es einmal mit
class AB : public A, public B { public: int A::f(); };
probiert?
Sollte das denn funktionieren?
|
Nein, die Lösung findest Du in D&E, Chapter 12.8:
class A1
{
virtual int A_f() {return A::f();}
}
Dann von A1 statt von A ableiten und A_f ueberschreiben
Helmut
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Stephan Keil Guest
|
Posted: Thu May 06, 2004 12:40 pm Post subject: Re: Multiple Inheritance |
|
|
| Quote: | Nein, die Lösung findest Du in D&E, Chapter 12.8:
class A1
{
virtual int A_f() {return A::f();}
}
Dann von A1 statt von A ableiten und A_f ueberschreiben
|
Hm, damit habe ich aber nicht A::f() ueberschrieben:
AB ab;
A* a = &ab;
a->f(); // hier wird A::f() aufgerufen, ich moechte aber AB::f()
aufrufen
Also doch keine Loesung :(
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Helmut Zeisel Guest
|
Posted: Thu May 06, 2004 12:55 pm Post subject: Re: Multiple Inheritance |
|
|
Stephan Keil wrote:
| Quote: | Nein, die Lösung findest Du in D&E, Chapter 12.8:
class A1
{
virtual int A_f() {return A::f();}
}
Dann von A1 statt von A ableiten und A_f ueberschreiben
Hm, damit habe ich aber nicht A::f() ueberschrieben:
AB ab;
A* a = &ab;
a->f(); // hier wird A::f() aufgerufen, ich moechte aber AB::f()
aufrufen
Also doch keine Loesung
|
Hast recht, ich war zu schlampig beim Abschreiben; richtig ist
class A1: public A
{
virtual int A_f() = 0;
virtual int f(){ return A_f();}
};
Wenn Du jetzt AB von A1 ableitest und A_f ueberschreibst, ruft Dein
a->f() wirklich A_f() auf.
Helmut
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Helmut Zeisel Guest
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|