CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206

    Question about function pointers

    I'm trying to create a typedef which can create a function pointer to a function which returns void and has no parameters, such as the example delcaration you see below:

    void TestFunc(void);

    I've tried the following:

    typedef void (*_FuncPtr)(void);

    So, if I do this:

    Code:
    _FuncPtr FunctionPtr = TestFunc;
    
    FunctionPtr();
    The compiler returns an error saying that FunctionPtr does not evaluate to a function taking 0 arguments.

    How would I appropriately create a function pointer such as this?

    Thanks!

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Question about function pointers

    I think the proper way to use a function pointer, to call the function that it is pointing to, is:
    Code:
    (*FunctionPtr)();
    Viggy

  3. #3
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206

    Re: Question about function pointers

    Viggy,

    I tried your suggested calling method, and my compiler returned the following error:

    error C2171: '*' : illegal on operands of type 'thu::_ThreadProc'


    Here is more code for your reference:

    Code:
    namespace thu
    {
            /* Typedefs */
        typedef void (*_ThreadProc)(void);
    };

  4. #4
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Question about function pointers

    MrDoomMaster, I can't see anything wrong with your code, your example actually works on VC6. Probably, it has something wrong with your definition of TestFunc().

    Code:
    #include <stdio.h>
    
    void TestFunc(void)
    {
    	printf("TestFunc\n");
    }
    
    typedef void (*_FuncPtr)(void);
    
    int main()
    {
    	_FuncPtr FunctionPtr = TestFunc;
    	FunctionPtr();
    
    	return 0;
    }

  5. #5
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206

    Re: Question about function pointers

    Well, maybe it will help if I post all of my code.

    It's not much, it's just a class I've created which makes creating threads a little simpler. I haven't had the chance to test it yet due to this compiler error, but perhaps it will give you a better chance of finding what I'm doing wrong. This class is contained in a Header file and CPP file:

    First, my Header File:
    Code:
    //=========================================================================
    //=========================================================================
    //          Multithreading Utilities Library (Thread_Utils)
    //
    //      Author: Robert Dailey (MrDoomMaster)
    //
    //=========================================================================
    //=========================================================================
    
    #ifndef                 _THREAD_UTILS_
    #define                 _THREAD_UTILS_
    
    //--Includes---------------------------------------------------------------
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    #include                <windows.h>
    
    //-------------------------------------------------------------------------
    
    
    
    //=============================================================================================
    // Namespaces
    //=============================================================================================
    /*
    ||
    */
    
    namespace thu
    {
            /* Typedefs */
        typedef void (*_ThreadProc)(void);
    };
    
    //=============================================================================================
    
    
    
    //=============================================================================================
    // Classes
    //=============================================================================================
    /*
    || This class provides the entire functionality of this library. It supports safe
    || multithreading.
    */
    class Thread_Utils
    {
        private:
            //--Variables-----------------------------------------------------------
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            bool                    ThreadRunning                               ;
            DWORD                   ThreadID                                    ;
            HANDLE                  ThreadHandle                                ;
            HANDLE                  StopHandle                                  ;
            HANDLE                  WaitHandle                                  ;
            thu::_ThreadProc        ThreadCode                                  ;
            //----------------------------------------------------------------------
    
    
            //--Prototypes----------------------------------------------------------
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            static                  DWORD WINAPI EntryPoint(LPVOID pThis);
            //----------------------------------------------------------------------
    
        public:
            //--Prototypes----------------------------------------------------------
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                /*--Constructors/Destructors--*/
            Thread_Utils(thu::_ThreadProc ThreadFunction);
            virtual ~Thread_Utils();
    
                /*--Functions--*/
            void                    StartThread(void);
            void                    StopThread(void);
            //virtual void            ThreadCode(void);
            //----------------------------------------------------------------------
    };
    
    //=============================================================================================
    
    #endif
    And my CPP file:

    Code:
    //=========================================================================
    //=========================================================================
    //          Multithreading Utilities Library (Thread_Utils)
    //
    //      Author: Robert Dailey (MrDoomMaster)
    //
    //=========================================================================
    //=========================================================================
    
    #include                "Thread_Utils.h"
    
    
    //===========================================================================================
    // Function Definitions
    //===========================================================================================
    /*
    || // TODO: Provide descriptions for all functions
    */
    DWORD WINAPI Thread_Utils::EntryPoint(LPVOID pThis)
    {
        while(true)
        {
            if(::WaitForSingleObject(((Thread_Utils*)pThis)->StopHandle, 0) == WAIT_OBJECT_0)
            {
                ::SetEvent(((Thread_Utils*)pThis)->WaitHandle);
                ::ExitThread(0);
            }
    
            ThreadCode();
        }
    }
    
    //===========================================================================================
    /*
    ||
    */
    Thread_Utils::Thread_Utils(thu::_ThreadProc ThreadFunction)
    {
        ThreadHandle    = NULL;
        StopHandle      = NULL;
        WaitHandle      = NULL;
        ThreadID        = 0;
        ThreadCode      = ThreadFunction;
    }
    
    //===========================================================================================
    /*
    ||
    */
    Thread_Utils::~Thread_Utils(void)
    {
        if(ThreadHandle)
            StopThread();
    }
    
    //===========================================================================================
    /*
    ||
    */
    void Thread_Utils::StartThread(void)
    {
        StopHandle      = ::CreateEvent(NULL, TRUE, FALSE, NULL);
        WaitHandle      = ::CreateEvent(NULL, TRUE, FALSE, NULL);
        ThreadHandle    = ::CreateThread(NULL, 0, EntryPoint, this, 0, &ThreadID);
    }
    
    //===========================================================================================
    /*
    ||
    */
    void Thread_Utils::StopThread(void)
    {
        ::SetEvent(StopHandle);
    
        ::WaitForSingleObject(WaitHandle, INFINITE);
    
        ::CloseHandle(StopHandle);
        ::CloseHandle(WaitHandle);
        ::CloseHandle(ThreadHandle);
    
        StopHandle      = NULL;
        WaitHandle      = NULL;
        ThreadHandle    = NULL;
    }
    
    //===========================================================================================
    
    //===========================================================================================
    
    //===========================================================================================
    
    //===========================================================================================
    
    //===========================================================================================
    
    //===========================================================================================
    
    //===========================================================================================
    
    //===========================================================================================
    
    //===========================================================================================
    
    //===========================================================================================
    I have bolded parts of my code which relate to the issue at hand, for your convenience. Thank you for your time, I hope my code isn't too rough to navigate!
    Last edited by MrDoomMaster; November 29th, 2004 at 08:17 PM.

  6. #6
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Question about function pointers

    You have made a small mistake in your static function. Your static function doesn't have the this pointer for invoking the function pointer. You need to invoke the it using pThis.
    Code:
    DWORD WINAPI Thread_Utils::EntryPoint(LPVOID pThis)
    {
        while(true)
        {
            if(::WaitForSingleObject(((Thread_Utils*)pThis)->StopHandle, 0) == WAIT_OBJECT_0)
            {
                ::SetEvent(((Thread_Utils*)pThis)->WaitHandle);
                ::ExitThread(0);
            }
    
            ((Thread_Utils*)pThis)->ThreadCode(); // this pointer isn't available in the static funciton.
        }
    }

  7. #7
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206

    Re: Question about function pointers

    Wow, thanks! I always miss stupid little things like that

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Question about function pointers

    Quote Originally Posted by MrDoomMaster
    error C2171: '*' : illegal on operands of type 'thu::_ThreadProc'
    Well...that is simply because 'ThreadProc()' is a class member function. There is a difference between function pointers to global and to class member functions...

  9. #9
    Join Date
    Jul 2004
    Posts
    1

    Re: Question about function pointers

    Your TestFunc is class member function? if yes, write to following:
    static void TestFunc()
    {
    .......
    }

  10. #10
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Question about function pointers

    Quote Originally Posted by MrViggy
    I think the proper way to use a function pointer, to call the function that it is pointing to, is:
    Code:
    (*FunctionPtr)();
    Viggy
    Also have a look at the "smart" pointers at Boost and Loki.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured