locked
callback function RRS feed

  • Question

  • hi i got this callback function from the web. and it work brilliantly

    im just having trouble trying to use a overloaded function with the class..

    heres my code

     

    help is very much appreciated thanks

    #pragma once
    
    class cCallback
    {
    	public:
    		virtual int Execute(const char *Param) const =0;
    		virtual int Execute(int index, const char *Param) const =0; // trying to overload the function Execute? so made 2 function with same name but different parameters
    };
    
    
    template <class cInstance>
    
    class TCallback : public cCallback // Inheriting
    {
    	public:
    
    		TCallback() // constructor
    		{
    			// Important : zeroing the pointer so we later can check for errors
    			pFunction = 0;
    		}
    
    		// (You can change the callback to take more parameters or to return something)
    		typedef int (cInstance::*tFunction)(const char *Param);
    		//typedef int (cInstance::*tFunction)(int index, const char *Param);
    		
    		// Execute the Callback
    		virtual int Execute(const char *Param) const 
    		{
    			if (pFunction) return (cInst->*pFunction)(Param);
    			else printf("ERROR : the callback function has not been defined !!!!");
    			// an undefined callback function will crash the program if you don't check here !
    			return 1;
    		}
    
    		virtual int Execute(int index, const char *Param) const 
    		{
    			if (pFunction) return (cInst->*pFunction)(index,Param);
    			else printf("ERROR : the callback function has not been defined !!!!");
    			// an undefined callback function will crash the program if you don't check here !
    			return 1;
    		}
    
    		// Set or change the Callback
    		void SetCallback (cInstance *cInstancePointer, 
    						 tFunction  pFunctionPointer)
    		{
    			cInst   = cInstancePointer;
    			pFunction = pFunctionPointer;
    		}
    
    	private:
    		cInstance *cInst;
    		tFunction pFunction;
    };
    
    
    
    

    Monday, July 26, 2010 10:06 AM

Answers

  • How are you actually trying to use these callbacks?  Are you trying somehow to use an abstract callback class?  If so, what you probably want is the Command Pattern,

    It won't work the way you show above, because the definition of tFunction is going to be tied to one or other of the function signatures.

    What you can do is make the function signature a template argument;  in this case, the underlying base class isn't going to be much use to you for defining function signatures.


    Answering policy: see profile.
    • Marked as answer by Yi Feng Li Monday, August 2, 2010 2:10 AM
    Monday, July 26, 2010 10:23 AM
  • Ugh, what a mess. Why not use std::tr1::function and avoid all this?
    • Marked as answer by Yi Feng Li Monday, August 2, 2010 2:10 AM
    Monday, July 26, 2010 11:51 AM

All replies

  • How are you actually trying to use these callbacks?  Are you trying somehow to use an abstract callback class?  If so, what you probably want is the Command Pattern,

    It won't work the way you show above, because the definition of tFunction is going to be tied to one or other of the function signatures.

    What you can do is make the function signature a template argument;  in this case, the underlying base class isn't going to be much use to you for defining function signatures.


    Answering policy: see profile.
    • Marked as answer by Yi Feng Li Monday, August 2, 2010 2:10 AM
    Monday, July 26, 2010 10:23 AM
  • i set the function like so

    i_CallbackOutput.SetCallback(this, &Socket::CallBack_Recv); // CallBack_Recv is a function which matches  Execute(const char *Param)

    p_CallbackOutput = &i_CallbackOutput;

     

    int CallBack_Recv(const char* callback)

    {

    cout << callback << endl;

    }

     

    and i call the function like so:

     p_CallbackOutput->Execute("Callback");

     

    it works when i use one function but when i try and overload it.. it doesnt.. for obvious reasons

     

     

     

    Monday, July 26, 2010 10:55 AM
  • Ugh, what a mess. Why not use std::tr1::function and avoid all this?
    • Marked as answer by Yi Feng Li Monday, August 2, 2010 2:10 AM
    Monday, July 26, 2010 11:51 AM
  • i dont have std::tr1::function on my computer
    Monday, July 26, 2010 12:24 PM
  • What version of VC++ are you using? There's always boost.function...
    Monday, July 26, 2010 12:50 PM
  • Hi Project-NZ,

    The std::tr1 is the Visual C++ implementation of the proposed extensions of the C++ ISO standard that are included in the draft document, "Technical Report 1." Normally, you can use function feature by  

     

    #include <functional>
    

     

    Cheers,

    Yi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, July 28, 2010 5:00 AM