Problem bei Zeiger auf Memberfunktion
-
Hall Leute!
Ich habe ein Problem bei einem Zeiger der auf eine Memberfunktion zeigen soll.class CD3dstuff { public: CD3dstuff(); //... private: class cProcessManager { // A structure that stores a function pointer and linked list typedef struct sProcess { void (*Function)(); sProcess *Next; } sProcess; protected: sProcess *m_ProcessParent; // The top state in the stack // (the head of the stack) public: cProcessManager() { m_ProcessParent = NULL; } ~cProcessManager() { sProcess *ProcessPtr; // Remove all processes from the stack while((ProcessPtr = m_ProcessParent) != NULL) { m_ProcessParent = ProcessPtr->Next; delete ProcessPtr; } } // Add function on to the stack void Add(void (*Process)()) { // Don't push a NULL value if(Process != NULL) { // Allocate a new process and push it on stack sProcess *ProcessPtr = new sProcess; ProcessPtr->Next = m_ProcessParent; m_ProcessParent = ProcessPtr; ProcessPtr->Function = Process; } } // Process all functions void Process() { sProcess *ProcessPtr = m_ProcessParent; while(ProcessPtr != NULL) { ProcessPtr->Function(); ProcessPtr = ProcessPtr->Next; } } }; // Ende von cProcessManager void CD_vSetAlpha(); // Funktion die an den Process Manager übergeben werden soll //... };
CD3dstuff::CD3dstuff() { cProcessManager InitMan,CleanUpMan; InitMan.Add(&CD3dstuff::CD_vSetAlpha); InitMan.Process(); }
Ich erhalte dann den Fehler:
error C2664: 'CD3dstuff::cProcessManager::Add' : cannot convert parameter 1 from 'void (__thiscall CD3dstuff::* )(void)' to 'void (__cdecl *)(void)'
There is no context in which this conversion is possibleUnd ich habe keine Ahnung wo genau der Fehler liegt...
Ich habe auch ein bischen herumprobiert:InitMan.Add(CD_vSetAlpha); //error C3867: 'CD3dstuff::CD_vSetAlpha': function call missing argument list; // use '&CD3dstuff::CD_vSetAlpha' to create a pointer to member InitMan.Add(this->CD_vSetAlpha); //error C2664: 'CD3dstuff::cProcessManager::Add' : // cannot convert parameter 1 from 'void' to 'void (__cdecl *)(void)'
und noch einige mehr. Diese haben aber alle nicht funktioniert.
-
Du kannst eine Methode nicht in einem 'normalen' Funktionszeiger unterbringen, dazu benötigst du einen Methodenzeiger. (der weiß im Gegensatz zum Funktionszeiger, daß er ein this-Objekt benötigt und wie er es übergeben kann)
typedef struct sProcess { void (C3Ddstuff::*Function)(); sProcess *Next; } sProcess; void Add(void(C3Ddstuff::*Process)()) {...}
Alterantiv könntest du mit statischen Methoden arbeiten und dir das "this"-Objekt von woanders besorgen.
PS:
typedef struct {...} name;
war vielleicht unter C notwendig. In C++ sieht das nur noch seltsam aus, bringt aber überhaupt nichts.