function pointer to a member function 
Author Message
 function pointer to a member function

I was trying to do the following:

class CTestingDoc : public CDocument {
public:
    ...
    UINT Testing(LPVOID pParam);
    int Caller();

Quote:
}

int CTestingDoc::Caller()
{
    UINT (* fp) (LPVOID);

    fp = Testing;

Quote:
}

and it gave me error of C2440: 'type cast' : cannot convert from '' to
'unsigned int (__cdecl *)(void *)'
if I put:
    fp = CTestingDoc::Testing;
error of C2440: '=' : cannot convert from 'unsigned int (__thiscall
CTestingDoc::*)(void *)' to 'unsigned int (__cdecl *)(void *)'

My guess is because of name convention (mangled) that C++ uses, that
differ from C.

Can anybody give me an explanation why wouldn't those statement work?
Is there any solution  without changing the fp definition, so that the
assignment is succesful?

thanks,
tim.

Note:
Actually, I was using it for the purpose of AfxBeginThread() function.



Sun, 27 Jan 2002 03:00:00 GMT  
 function pointer to a member function
Go to this link and read:

http://www.cerfnet.com/~mpcline/c++-faq-lite/pointers-to-members.html

You might find other useful stuff there as well.


Quote:
> I was trying to do the following:

> class CTestingDoc : public CDocument {
> public:
>     ...
>     UINT Testing(LPVOID pParam);
>     int Caller();
> }

> int CTestingDoc::Caller()
> {
>     UINT (* fp) (LPVOID);

>     fp = Testing;
> }

> and it gave me error of C2440: 'type cast' : cannot convert from '' to
> 'unsigned int (__cdecl *)(void *)'
> if I put:
>     fp = CTestingDoc::Testing;
> error of C2440: '=' : cannot convert from 'unsigned int (__thiscall
> CTestingDoc::*)(void *)' to 'unsigned int (__cdecl *)(void *)'

> My guess is because of name convention (mangled) that C++ uses, that
> differ from C.

> Can anybody give me an explanation why wouldn't those statement work?
> Is there any solution  without changing the fp definition, so that the
> assignment is succesful?

> thanks,
> tim.

> Note:
> Actually, I was using it for the purpose of AfxBeginThread() function.



Sun, 27 Jan 2002 03:00:00 GMT  
 function pointer to a member function
change your Caller function like this:

int CTestingDoc::Caller()
{
    UINT (CTestingDoc::* fp) (LPVOID);        <-- change here
    fp = Testing;

Quote:
}

That way, you tell the compiler that fp is a pointer to a member function of
the class CTestingDoc. To call a function through a member function pointer,
the syntax is

CTestingDoc * td = <some initial value>;
UINT (CTestingDoc::* fp) (LPVOID) = <some initial value>;

UINT i = (td -> * fp) ();

Note that you need an object pointer _and_ a member function pointer to do
the call.

tr.

I was trying to do the following:

class CTestingDoc : public CDocument {
public:
    ...
    UINT Testing(LPVOID pParam);
    int Caller();

Quote:
}

int CTestingDoc::Caller()
{
    UINT (* fp) (LPVOID);

    fp = Testing;

Quote:
}

and it gave me error of C2440: 'type cast' : cannot convert from '' to
'unsigned int (__cdecl *)(void *)'
if I put:
    fp = CTestingDoc::Testing;
error of C2440: '=' : cannot convert from 'unsigned int (__thiscall
CTestingDoc::*)(void *)' to 'unsigned int (__cdecl *)(void *)'

My guess is because of name convention (mangled) that C++ uses, that
differ from C.

Can anybody give me an explanation why wouldn't those statement work?
Is there any solution  without changing the fp definition, so that the
assignment is succesful?

thanks,
tim.

Note:
Actually, I was using it for the purpose of AfxBeginThread() function.



Mon, 28 Jan 2002 03:00:00 GMT  
 function pointer to a member function
Hi,
thanks for all the replies...

I have more question related to this one:
The following is what I did:

class CTestingDoc : public CDocument {
public:
    ...
    static UINT Testing(LPVOID pParam);
    HRESULT MakeNewThread();

protected:
    CWinThread * pNewThread;

Quote:
}

HRESULT CTestingDoc::MakeNewThread()
{
    pNewThread = (CWinThread *) AfxBeginThread(Testing, NULL);

    return 0;

Quote:
}

UINT CTestingDoc::Testing(LPVOID pParam)
{
    int i;

    i = 5;
    return;

Quote:
}

The question that I have:
1. Notice the Testing() function, when I made them static, I can pass it
to the AfxBeginThread. So, I can "mix" between global function and member
function, if I made them static - well at least for this AfxBeginTread. Is
there any implication (good or bad), problem, or anything that I am not
aware of? Can somebody tell me please...
2. If I call AfxBeginThread multiple times, with the same parameters,
Is it correct that all the newly created thread see the same variable i,
hence the end result of i is undefined?
Is it correct that everything (except using Thread Local Storage), are
accessible by threads?

thanks,
tim.

Quote:
> int CTestingDoc::Caller()
> {
>     UINT (* fp) (LPVOID);

>     fp = Testing;
> }

> and it gave me error of C2440: 'type cast' : cannot convert from '' to
> 'unsigned int (__cdecl *)(void *)'
> if I put:
>     fp = CTestingDoc::Testing;
> error of C2440: '=' : cannot convert from 'unsigned int (__thiscall
> CTestingDoc::*)(void *)' to 'unsigned int (__cdecl *)(void *)'

> My guess is because of name convention (mangled) that C++ uses, that
> differ from C.

> Can anybody give me an explanation why wouldn't those statement work?
> Is there any solution  without changing the fp definition, so that the
> assignment is succesful?

> thanks,
> tim.

> Note:
> Actually, I was using it for the purpose of AfxBeginThread() function.



Tue, 29 Jan 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. function pointer to a member function

2. function pointer to a member function

3. How to use (pointer to function), and function and pointer to (pointer to function)

4. Pointers to member functions as function arguments

5. (member) function returning pointer to functions like itself?

6. Getting pointer to non-static member function from C callback function

7. Function Pointer for Member Function

8. Function pointer for Member Function

9. Function pointer / function member

10. Function Pointers to Member Functions

11. How to pass a member function as inetrnal function pointer argument

12. member functions as function pointers!

 

 
Powered by phpBB® Forum Software