instanz einer klasse nur einmal
-
ich suche wie ein verrückter schon den ganzen tag einen miesen fehler.
irgendwo ist nen speicherfehler und ich find ihn nicht .. irgendwie bin ich jetzt aber fest der meinung, das es meinen klassen liegt.ich habe eine Key, Mouse und gloVars (noch mehr) Klassen.
da man diese nur einmal benötigt (gibt ja nur eine tastatur etc am pc) habe ich sie als einmalige instanz gemacht.#ifndef __GLOBALES__ #define __GLOBALES__ class Mouse { public: int GetMPosX() { return m_mousePosX; } int GetMPosY() { return m_mousePosY; } static Mouse& GetInstanz() { static Mouse instanz; return instanz; } void Clear() { m_mousePosX = 0; m_mousePosY = 0; } private: int m_mousePosX; int m_mousePosY; Mouse() { Clear(); } Mouse(const Mouse&) {}; Mouse& operator=(const Mouse&); }; //-------------------------------------------------------- class Keys { public: void SetKeyCode(char key) { k_KeyCode = key; } char GetKeyCode() { return k_KeyCode; } static Keys& GetInstanz() { static Keys instanz; return instanz; } void Clear() { k_KeyCode = 0x00; } private: char k_KeyCode; Keys() { Clear(); } Keys(const Keys&) {}; Keys& operator=(const Keys&); }; //-------------------------------------------------------- class gloVars { public: static gloVars& GetInstanz() { static gloVars instanz; return instanz; } int glo_int; float glo_float; char glo_char[256]; string glo_string; bool glo_bool; vector<string> glo_vector; CTexture material; bool glo_moveElements; private: gloVars() { glo_int = 0; glo_float = 0.0f; glo_char[255] = 0x00; glo_string = ""; glo_bool = false; glo_moveElements = true; } gloVars(const gloVars&) {}; gloVars& operator=(const gloVars&); }; #endif
im programm ruf ich dann überall direkt auf:
if (Keys::GetInstanz().SetKeyCode(wParam)=='c')
oder
int x = Mouse::GetInstanz().GetMPosX();
oder
gloVars::GetInstanz().glo_int = 7;ist das so richtig und bringt keine speicheraddressprobleme mit sich?
ich bin total ratlos langsam ....falls wer hier der ahnung hat sich erbarmen will und mal den quelltext überfliegen, schick ich ihm diesen mit paar zeilen wo der speicherfehler sich bei mir bemerkbar macht. das nicht mein programm, darum kann ich die quellen nicht allgemein zur verfügung stellen zumal sein(unser) "spiel" noch total in den kinderschuhen steckt und nur an der oberflächensteuerung (menü) gearbeitet wird.
-
Hallo,
da man diese nur einmal benötigt (gibt ja nur eine tastatur etc am pc) habe ich sie als einmalige instanz gemacht.
.. ich verstehe dein Problem nicht.
Wenn du nur eine Instanz von der Klasse brauchst, dann such bei Google nach
'Singleton Pattern'.Müsste ungefähr so aussehen:
class Mouse { private: static Mouse* theInstance; Mouse(){ } // private! public: static Mouse* getInstance { if(theInstance==0) theInstance = new Mouse(); return theInstance; } };
Aufruf:
Mouse* Mouse::theInstance = 0; // 0 initialisieren Mouse *mouse = Mouse::getInstance();
Dann einfach mit
mouse->
weiterarbeiten.
Ich hoffe das hilft dir ein bißchen weiter..
-
Das Meyers-Singleton ist auf Humes Seite einzusehen. Den Link habe ich auch gerade nicht.
Stell dir aber vorher mal die Frage, ob die Klasse wirklich nur einmal benötigt wird. Und wenn, dann überlege doch, ob es nicht einfach nur eine Beraubung der Möglichkeiten wäre, wenn du ein Singleton draus machst, es gibt vielleicht meistens nur eine Tastatur, aber wieso sollte es die Möglichkeit von 2 Tastaturen nicht geben?
MfG MAV
-
Sonst, mach den Destruktor protected, setze nen friend zu ner Funktion, die ein Statikobjekt deiner Klasse erzeugt.
mfg