Member function pointer

L

leaf

How do i 'cast' as a parameter a 'pointer to a member function' of the
class to a function of a member class object of a class?

Here's my example:
//------------------------------------------------------------------------------------------------
class MyClass
{
public:
void OnEvent( LPVOID lpParam );
void SetCallBack( void(__cdecl *fCallBack)(LPVOID lpParam) );
private:
void(__cdecl *m_CallBack)(LPVOID lpParam);
}

void MyClass::SetCallBack( void(__cdecl *fCallBack)(LPVOID lpParam) )
{
m_CallBack = fCallBack;
}

void MyClass::OnEvent( LPVOID lpParam )
{
m_CallBack( lpParam ); // Call the callback function
}
//------------------------------------------------------------------------------------------------
class MyClassMgr
{
public:
static void WINAPI fRealCallBack( LPVOID lpParam );
.....
private;
MyClass m_myclass;
}
// During initialization of MyClassMgr
MyClassMgr::MyClassMgr( )
{
myclass.SetCallBack( &MyClassMgr::fRealCallBack /* ???*/ ) ; //
this part is what i fully understand
}

young_leaf
 
A

Alf P. Steinbach

* leaf:
How do i 'cast' as a parameter a 'pointer to a member function' of the
class to a function of a member class object of a class?

Here's my example:
//------------------------------------------------------------------------------------------------
class MyClass
{
public:
void OnEvent( LPVOID lpParam );

Please remove inessential nonstandardism's before posting here.

void SetCallBack( void(__cdecl *fCallBack)(LPVOID lpParam) );
private:
void(__cdecl *m_CallBack)(LPVOID lpParam);
}

void MyClass::SetCallBack( void(__cdecl *fCallBack)(LPVOID lpParam) )
{
m_CallBack = fCallBack;
}

void MyClass::OnEvent( LPVOID lpParam )
{
m_CallBack( lpParam ); // Call the callback function
}
//------------------------------------------------------------------------------------------------
class MyClassMgr
{
public:
static void WINAPI fRealCallBack( LPVOID lpParam );
....
private;
MyClass m_myclass;
}
// During initialization of MyClassMgr
MyClassMgr::MyClassMgr( )
{
myclass.SetCallBack( &MyClassMgr::fRealCallBack /* ???*/ ) ; //
this part is what i fully understand

This should not compile because 'myclass' is nowhere defined.

Please describe what the problem is, and post a complete smallest
possible program that compiles and exhibits the problem, or, if the
problem is that it doesn't compile, post a smallest possible program
that demonstrates the compilation error, along with the error message.
 
A

amparikh

leaf said:
How do i 'cast' as a parameter a 'pointer to a member function' of the
class to a function of a member class object of a class?

Here's my example:
//------------------------------------------------------------------------------------------------
class MyClass
{
public:
void OnEvent( LPVOID lpParam );
void SetCallBack( void(__cdecl *fCallBack)(LPVOID lpParam) );
private:
void(__cdecl *m_CallBack)(LPVOID lpParam);
}

void MyClass::SetCallBack( void(__cdecl *fCallBack)(LPVOID lpParam) )
{
m_CallBack = fCallBack;
}

void MyClass::OnEvent( LPVOID lpParam )
{
m_CallBack( lpParam ); // Call the callback function
}
//------------------------------------------------------------------------------------------------
class MyClassMgr
{
public:
static void WINAPI fRealCallBack( LPVOID lpParam );
....
private;
MyClass m_myclass;
}
// During initialization of MyClassMgr
MyClassMgr::MyClassMgr( )
{
myclass.SetCallBack( &MyClassMgr::fRealCallBack /* ???*/ ) ; //
this part is what i fully understand
}

young_leaf

Not 100% sure what your design is but based on what you have written, I
would have something like this...

class MyClass
{
public:
void OnEvent( LPVOID lpParam );
typedef void (*fCallBack)(LPVOID lpParam);
template <typename Func>
void SetCallBack( Func CallBack )
{
m_CallBack = CallBack;//reinterpret_cast<fCallBack>(CallBack);
}
private:
fCallBack m_CallBack;
};

void MyClass::OnEvent( LPVOID lpParam )
{
m_CallBack( lpParam ); // Call the callback function
}


//-------------------------------------------------------------------------­-----------------------

class MyClassMgr
{
public:
static void fRealCallBack( LPVOID lpParam );
MyClassMgr();
private:
MyClass m_myclass;
};

// During initialization of MyClassMgr
MyClassMgr::MyClassMgr()
{
m_myclass.SetCallBack( &MyClassMgr::fRealCallBack );
}

void MyClassMgr::fRealCallBack( LPVOID lpParam ){ }


int main()
{
MyClassMgr mgr;
}
 
L

leaf

I get an unresolved external symbol error when i compile that code:

error LNK2001: unresolved external symbol "private: void * __cdecl
MyClass::m_CallBack(LPVOID)" (?m_CallBack@x@@AAAPAXH@Z)

Any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top