Thread View: de.comp.lang.c++
2 messages
2 total messages
Started by Thomas Trenz
Thu, 16 Nov 1995 10:45
"Uberladen des Ausgabeoperators f"ur Streams.
Author: Thomas Trenz
Date: Thu, 16 Nov 1995 10:45
Date: Thu, 16 Nov 1995 10:45
68 lines
1695 bytes
1695 bytes
Hi Leute,
ich arbeite mit Exception-Klassen und habe f"ur jede Exception-Klasse
einen Ausgabe-Operator f"ur Streams geschrieben. Kann man bei dem
Operator << auch den virtuellen Mechanismus verwenden, so daß das
folgende Beispiel die "richtige" Ausgabe liefert?
class ExceptionBase
{
};
class ExceptionA
{
};
class ExceptionB
{
};
ostream & operator << (ostream & os, const ExceptionBase &b)
{
os << "Base" << endl;
}
ostream & operator << (ostream & os, const ExceptionA &b)
{
os << "ExceptionA" << endl;
}
ostream & operator << (ostream & os, const ExceptionB &b)
{
os << "ExceptionB" << endl;
}
Jetzt der C++-Code:
void Testfunktion (int Parameter) throw (ExceptionBase)
{
switch (Parameter)
{
case 0 : throw ExceptionA();
case 1 : throw ExceptionB();
}
}
int main (int argc, char *argv[])
{
try
{
Testfunktion (1);
}
catch (ExceptionBase &e) { cout << e << endl; }
}
Ausgabe:
Base
Ist es möglich, das Programm so zu modifizieren, daß die Fehlermeldung
des abgeleiteten Typs ausgegeben wird?
Also ist es möglich irgendwie eine Art virtuellen Ausgabeoperator zu
schreiben?
Vielen Dank schon mal...
Re: "Uberladen des Ausgabeoperators f"ur Streams.
Author: Ralf W. Stephan
Date: Fri, 17 Nov 1995 10:22
Date: Fri, 17 Nov 1995 10:22
25 lines
940 bytes
940 bytes
Thomas Trenz writes:
> Ist es möglich, das Programm so zu modifizieren, daß die Fehlermeldung
> des abgeleiteten Typs ausgegeben wird?
Das sollte so funktionieren, nur hast Du vergessen, die Klassen abzuleiten:
> class ExceptionA : public ExceptionBase
> {
> };
> class ExceptionB : public ExceptionBase
> {
> };
Ich persönlich würde es eh anders machen, aber das ist Geschmaxache.
Beispiel:
catch (const ExceptionBase& e) { e.print(); }
...mit einem virtuellen print() member.
ralf
--
PGP: 1024/0xA713ECE9 2047/0xC8E605F5
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads