I’m wrapping an existing C API to make it easier to use in my VS2008 C++ program. The C API is expecting an array of “TABLE_ENTRY” structures that include a function pointer as in the code below.
But, I’m having difficulty storing a pointer to a member function in the function pointer.
Can anybody point out what I may be doing wrong?
Thanks,
PaulH
My code looks basically like this:
struct TABLE_ENTRY; // forward decl
typedef int (WINAPI *MYPROC )(DWORD msg, TABLE_ENTRY* entry);
struct TABLE_ENTRY {
const char* description;
DWORD value;
MYPROC callback;
};
class MyClass
{
public:
MyClass() : description( "Some Description" ),
some_value( 1 )
{
};
int MyProc( DWORD msg, TABLE_ENTRY* my_entry )
{
return 0;
};
TABLE_ENTRY* operator*()
{
entry_.description = description.c_str();
entry_.value = some_value;
// error C2440: '=' : cannot convert from 'boost::_bi::bind_t<R,F,L>' to 'MYPROC'
entry_.callback = boost::bind< int >( &MyClass::MyProc, this );
return &entry_;
};
TABLE_ENTRY entry_;
std::string description;
DWORD some_value;
};
class MyClassCollection
{
public:
TABLE_ENTRY* GetTable()
{
// is this okay or is it Evil & wrong?
return ( &collection_.front() )->operator*();
};
void Add( MyClass& my_class )
{
collection_.push_back( my_class );
}
private:
std::vector< MyClass > collection_;
};
int _tmain( int argc, _TCHAR* argv[] )
{
MyClass class1;
MyClass class2;
MyClassCollection collection;
collection.Add( class1 );
collection.Add( class2 );
TABLE_ENTRY* table = collection.GetTable();
TABLE_ENTRY entry1 = table[ 0 ]; // should be class1's table
TABLE_ENTRY entry2 = table[ 1 ]; // should be class2's table
return 0;
}
boost::bindcreates a functor, i.e. an instance of a class that implementsoperator(). This is not interchangeable with plain C function pointers.