Click to See Complete Forum and Search --> : Pointer to member functions in VS C++ 2003


ferenckv
July 13th, 2007, 03:30 PM
I have this code:


typedef void (*DevRespPointer) (BlueDevice __gc*);

class InternalBlueManager : public CBtIf
{
public:
DevRespPointer DeviceRespondedDelegate;

InternalBlueManager(void)
{
}

~InternalBlueManager(void)
{
}

//Implementacion de Funciones Virtuales Heredadas

void OnDeviceResponded (BD_ADDR bda, DEV_CLASS devClass, BD_NAME bdName, BOOL bConnected)
{

if (DeviceRespondedDelegate!=0)
{
BlueDevice *bd = new BlueDevice(bda, devClass, bdName);
DeviceRespondedDelegate(bd);
}
}

};


/////////////////////////////////////////////


public __gc class BlueManager
{

private: //Miembros Privados
InternalBlueManager * blue;


public: // Funciones Publicas

BlueManager(void)
{
blue = new InternalBlueManager();

}

~BlueManager(void)
{

}

void SetDelegates()
{
******blue->DeviceRespondedDelegate = OnDeviceResponded;*****
}

private: //Funciones Privadas

void OnDeviceResponded(BlueDevice __gc *device)
{
// Do Something;
}

};


If I compile this I get this error message on the **** line:
error C2440: '=' : cannot convert from 'void (__clrcall BlueClasses::BlueManager::* )(BlueClasses::BlueDevice __gc *)' to 'BlueClasses:: DevRespPointer'


If I change the red line for

blue->DeviceRespondedDelegate = &OnDeviceResponded;

I get this error:
error C2276: '&' : illegal operation on bound member function expression

If I change it for

blue->DeviceRespondedDelegate = &BlueManager:: OnDeviceResponded;

I get this error:
error C3374: managed function 'BlueClasses::BlueManager:: OnDeviceResponded' requires argument list.

And if I change it for

blue->DeviceRespondedDelegate = &(BlueManager:: OnDeviceResponded);

I get this error again:
error C2276: '&' : illegal operation on bound member function expression.

I'm compiling it with Visual Studio 2003, Framework 1.1

This is what I'm trying to do:
I have a unmanaged c++ class which has a method onDeviceResponded.
I have a managed c++ class which also has a method onDeviceResponded, and I want to call the managed class method when the unmanaged class method is called, I'm trying to do this by pointing a function pointer in the unmanaged class to the managed class onDeviceResponded method, I'm thinking this like somekind of "event" in the unmanaged class which calls the managed class method.

Now I'm declaring DevRespPointer this way:

typedef void (*DevRespPointer) (BlueDevice __gc*);

If I declare it this way

typedef public void (BlueManager::*DevRespPointer) (BlueDevice __gc*);

I get this error:
error C2843: 'BlueClasses::BlueManager' : cannot take the address of a non-static data member or method of a managed type

If I can't do this the way I want, Is there another way to do this "event" thing?