Thread: Problem with MFP and bound member

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    3

    Problem with MFP and bound member

    [C++/Win32/WinXP/.NET7.0]
    I'm trying to write a threading class for my socket library, and I get compiler errors every time I try to use a function pointer. This is the interface for the class:

    Code:
    class Threader : public ThreadABC {
    public:
    	typedef void (*pf) (void);
    	Threader( void (*pf) (void) ) : pfunction(pf)
    	{ 
    		begin(); 
    	}
    private:
    	void (*pfunction) (void);
    	void ThreadProc() {
    		pfunction();
    	}
    };
    begin() calls the CreateThread Win32 function, which in turn calls ThreadProc() (this is to get around the Windows problem with calling a member function).

    This is the implementation that I try to use to instantiate the class:

    Code:
    void (*pc) (void);
    pc = &(PackConnections);
    ConnectionPacker = new Threader( pc );
    This code brings up the compiler error C2276:
    Code:
    error C2276: '&' : illegal operation on bound member function expression
    This error message points to the second line in the implementation (pc = &(PackConnections);).

    Does anybody have any ideas?

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Assuming the function is the correct prototype, then

    pc = PackConnections;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    3
    I replaced
    Code:
    pc = &(PackConnections);
    with
    Code:
    pc = (PackConnections);
    and now I'm getting an even scarier error:

    Code:
    error C2440: '=' : cannot convert from 'void (__thiscall SockServer::* )(void)' to 'void (__cdecl *)(void)'
    I'm not even sure what that means...

    (PackConnections is a method of SockServer, btw)

  4. #4

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    3
    I've fixed the implementation with the following code:
    Code:
    void (SockServer::*pc) (void) = (&SockServer::PackConnections);
    ConnectionPacker = new Threader( pc );
    However, now the constructor definition seems to be the wrong type. I don't fully understand what this means:
    Code:
    error C2664: 'Threader::Threader(void (__cdecl *)(void))' : cannot convert parameter 1 from 
    'void (__thiscall SockServer::* )(void)' to 'void (__cdecl *)(void)'
    As you may recall, the constructor is as follows:
    Code:
    Threader( void (*pf) (void) )
    { 
    	... 
    }
    What should the argument of the constructor be to be type-safe? Also, how should I cast so I can make the implementation type-safe?

    Thanks

  6. #6

Popular pages Recent additions subscribe to a feed