Say I have an array of void pointer pointing to objects of different types and I want to pass them through a function and cast them to pointers of their respective type, how would I go about doing this? I've tried this current code, but I get error:
‘void*’ is not a pointer-to-object type
objectA myObjectA;
objectB myObjectB;
objectC myObjectC;
objectD myObjectD;
void *data[4];
data[0] = static_cast<void *>( &myObjectA );
data[1] = static_cast<void *>( &myObjectB );
data[2] = static_cast<void *>( &myObjectC );
data[3] = static_cast<void *>( &myObjectD );
void dereference( void *data )
{
objectA *objA_ptr = static_cast<objectA *>( data[0] );
objectB *objB_ptr = static_cast<objectB *>( data[1] );
objectC *objC_ptr = static_cast<objectC *>( data[2] );
objectD *objD_ptr = static_cast<objectD *>( data[3] );
}
Anyone know the correct way to implement this?
edit: For context, I'm using opencv and trying to create a simple gui interface with a trackbar. Opencv allows for userdata to be fed into the TrackbarCallback which is called when the slider is moved, but only in the form of a void pointer.
int createTrackbar( const string& trackbarname, const string& winname,
int* value, int count,
TrackbarCallback onChange=0,
void* userdata=0);
void*at all? This is C++, you have templates.