Wrapperklasse für Windows HFONT - Kommentare erwünscht
-
Ich schreibe gerade eine Wrapper-Klasse für Fonts: http://mitglied.lycos.de/fatbull84/pub/Font.h
Verwendet wird das ganze z.B. so:gdi::Font fnt("Courier New"); gdi::FontHandle fh(fnt); gdi::FontHandle fh2(fh); gdi::Font fnt2(fnt); gdi::Font fnt3(fh); gdi::Font f(gdi::AttachFont(CreateFont(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)));
Was haltet ihr davon? Fehler? Vorschläge? Was kann man besser machen?
-
Warum nicht so?
template<bool> class FontT; typedef FontT<true> Font; typedef FontT<false> FontHandle; template<bool D> class FontT { public: FontT(HFONT hFont = 0) : m_hFont(hFont) { } ~virtual FontT() { if (D && m_hFont) Delete(); } FontHandle Duplicate() const { //... return newfont; } bool Delete() { bool r = m_hFont != 0; if (r) { ::DeleteObject(m_hFont); m_hFont = 0; } return r; } operator HFONT() const { return m_hFont; } FontT& operator=(HFONT hFont) { if (m_hFont != hFont) { if (D && m_hFont) Delete(); m_hFont = hFont; } return *this; } private: FontT(const FontT&) { } FontT& operator=(const FontT&) { return *this; } HFONT m_hFont; };
Somit hast du ein selbstzerstörbares und ein normales Handle in einer Klasse. Protected Datenelemente solltest du vermeiden, sonst könnte praktisch jeder daran rumfuchteln. Die Implementierung des rein-virtuellen Dtors muss von der Deklaration getrennt werden.
-
Warum nicht so?
Ich habe es nicht wie in der ATL gemacht (die machen das ganz ähnlich wie du), weil ich nicht wollte, dass ich ein FontHandle einem Font zuweisen kann. Ich will klare Verhältnisse, wer die Schriftart hinterher wieder löscht und ich will es möglichst schwer machen, eine Schriftart doppelt zu löschen.
Protected Datenelemente solltest du vermeiden, sonst könnte praktisch jeder daran rumfuchteln.
Hab ich mir auch überlegt... Aber auf der anderen Seite mag ich auch nicht unbedingt für jeden Zugriff eine virtuelle Funktion aufrufen für so eine relativ "billige" Wrapper-Klasse.
Die Implementierung des rein-virtuellen Dtors muss von der Deklaration getrennt werden.
Das ist mir neu... Wo steht das?
-
tag schrieb:
Das ist mir neu... Wo steht das?
ISO/IEC 14882 §10.4. http://www.kuzbass.ru:8086/docs/isocpp/derived.html
a function declaration cannot provide both a pure-specifier and a definition
-
Shlo schrieb:
tag schrieb:
Das ist mir neu... Wo steht das?
ISO/IEC 14882 §10.4. http://www.kuzbass.ru:8086/docs/isocpp/derived.html
a function declaration cannot provide both a pure-specifier and a definition
Alles klar, danke