473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Member function pointer

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

Mar 7 '06 #1
3 1975
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 7 '06 #2

leaf wrote:
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;
}

Mar 7 '06 #3
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?

Mar 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
3
by: dice | last post by:
Hi, In order to use an external api call that requires a function pointer I am currently creating static wrappers to call my objects functions. I want to re-jig this so I only need 1 static...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary function...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.