calling a member function pointer with an structure´s assistance

I am passing a structure to an thread.
The structure contains a pointer to Object A and an function pointer to one of A´s functions...

No I am wondering how to call it:

Here is what i got, but that resolves in an error
MSVC wrote:
Error C2064: term does not evaluate to a function taking 1 arguments47


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Class A
{
B *pB;    //assume its pointing to an B
void somefunc()    //This will be called by the user or something else
{
B.Start(&A::TheEvilFunc, this);
};

TheEvilFunc(std::string);
}
};

class B
{
struct X
{
A *pA;
void (A::*pFunc)(std::string);
};

void Start(void (A::*pfunc)(std::string), A* pA)
{
X THEX;
THEX.pFunc = pfunc;
THEX.pa = pa;

CreateThread(NULL, 0, SomeThreadFunc, THEX , 0, NULL);
}

friend DWORD WINAPI SomeThreadFunc(void*);
}

DWORD WINAPI SomeThreadFunc(void* v)
{
//blabla
X x; //assume its been assigned v

///////////////////// Now how to call the pFunc?
(x.pClient)->*(x.pFunc(strRecvd));     ///Here goes the error
}
Last edited on
//The windows version

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "stdafx.h"  //I'm using visual studio - that's why this is here

#include <iostream>
#include <string>

using namespace std;

class A;  //Forward declaration

class B
{
public:
    
    struct X
    {
        A *pA;
        void (A::*pFunc)(std::string);
    };

    HANDLE  Start(void (A::*pfunc)(std::string), A* pA)
    {
        THEX.pFunc = pfunc;
        THEX.pA = pA;

        return CreateThread(NULL, 0, SomeThreadFunc, &THEX , 0, NULL);
    }

    X THEX;  //we need to put this variable here - not in the start function

    friend DWORD WINAPI SomeThreadFunc(void*);
};

class A
{
public:
    
    B *pB;    //

    A()
    {
        pB = new B;
    }
    ~A()
    {
        delete pB;
    }
            
    HANDLE  somefunc()    //Changde to return a handle
    {
        return pB->Start(&A::TheEvilFunc, this);
    };

    void TheEvilFunc(std::string  str)
    {
        cout << str << endl;
    }

};


//The thread function
DWORD WINAPI SomeThreadFunc(void* v)
{
 
    B::X *px = (B::X *)v;

    ((px->pA)->*(px->pFunc))("Hello World");
    
    return 0;
}


int main(int argc, char* argv[])
{
    A a;
 
    HANDLE h = a.somefunc();
    
    WaitForSingleObject(h,INFINITE);
    
    cin.get();
    
    return 0;
}
Topic archived. No new replies allowed.