Wenn fertig, bitte Fenster schließen
Inhalt der Datei demo.h
// Demo-Klasse für Template-Anwendungen // Erfüllt die Regel der Grossen 3 #include <string> #include <iostream> // Makro für Debug-Ausgaben //#define TRACE_METH #ifdef TRACE_METH #define TRACE(T_) std::cout << "Obj-Nr" << num << ": " << T_ << std::endl #else #define TRACE(T_) #endif class Demo { std::string text; static int count; int num; public: Demo(); Demo(const char* pText); Demo(const Demo& obj); virtual ~Demo(); Demo& operator = (const Demo& rhs); bool operator < (const Demo& rhs) const; bool operator == (const Demo& rhs) const; friend bool operator < (const Demo& lhs, const Demo& rhs); friend bool operator == (const Demo& lhs, const Demo& rhs); friend std::ostream& operator << (std::ostream& os, const Demo& obj); }; // ========================================================================== // In der Praxis sollten Sie in einer Header-Datei keine Memberfkt. definieren // da es sonst beim Einbinden der Header-Datei in mehreren C++ Quelldateien // zu doppelt-definierten Memberfunktionen kommt. Dies dient hier einzig und allein // zur Vereinfachung des Compile-Vorgangs und entspricht daher in keiner // Weise der alltäglichen Praxis! // ========================================================================== // statische Objektzähler int Demo::count=0; // ctors Demo::Demo() { num = ++count; TRACE("std-ctor"); } Demo::Demo(const char* pText): text(pText) { num = ++count; TRACE("ctor"); } Demo::Demo(const Demo& obj): text(obj.text) { num = ++count; TRACE("copy-ctor"); } // dtor Demo::~Demo() { TRACE("dtor"); } // Zuweisungsoperator Demo& Demo::operator = (const Demo& rhs) { if (&rhs==this) return *this; text = rhs.text; TRACE("operator="); return *this; } // Vergleichsoperatoren, werden erst für STL-Anwendung benötigt bool operator < (const Demo& lhs, const Demo& rhs) { return lhs.text < rhs.text; } bool operator == (const Demo& lhs, const Demo& rhs) { return lhs.text == rhs.text; } bool Demo::operator < (const Demo& rhs) const { return text < rhs.text; } bool Demo::operator == (const Demo& rhs) const { return text == rhs.text; } // Ausgabeoperator std::ostream& operator << (std::ostream& os, const Demo& obj) { std::cout << obj.text; return os; } |