problem with a funtion in pointer as part of a struct in a class

Hi!

ihave the following problem. I have written a class that contains a struct and a function. The struct contains a pointer to that function. This struct shall be passed as a void-pointer into a static function of the class and this static function calls some parameters from the same class.

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
#include <iostream>

using namespace std;

class  someclass
{
    public:
    someclass()
    {
        s1 = new example;
        s1->data = "test1";
        s1->temp = "test2";
        s1->fun1 = &someclass::firstFun;
    }
    ~someclass();
    struct example
    {
        const char    *data;
        const char    *temp;
        void (someclass::*fun1)(const char* , const char* );
    } *s1;

    static void staticFun(void* param)
    {
        example* ex = (example*)param;
        ex->*fun1( ex->data, ex->temp);
    }

    void firstFun(const char* val1, const char* val2)
    {
        cout << val1 << " " << val2 << endl;
    }

} ;

int main()
{
    someclass* cl = new someclass;
    cl->staticFun((void*)cl->s1);
    delete c1;

} 

If i call this static function then i get the following output
error: 'fun1' was not declared in this scope


I hope somebody here can help me.
Thank you and greetings,
Ingeborg
Maybe you wanted something like this:
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
//===========================================================================================

class  someclass
{
    public:
    someclass()
    {
        s1 = new example;
        s1->data = "test1";
        s1->temp = "test2";
        s1->fun1 = &someclass::firstFun;
    }
    ~someclass() {};  //remember to put the actual body of the  destructor.
    
    struct example
    {
        const char    *data;
        const char    *temp;
        void (someclass::*fun1)(const char* , const char* );
    } *s1;

    static void staticFun(void* param)
    {
              
        //example* ex = (example*)param; //error
        //ex->*fun1( ex->data, ex->temp);//error

        someclass* cl = (someclass*) param;
        (cl->* (cl->s1->fun1))( cl->s1->data, cl->s1->temp);
        
    }

    void firstFun(const char* val1, const char* val2)
    {
        cout << val1 << " " << val2 << endl;
    }


} ;

//============================================================================================

int main()
{
    someclass* cl = new someclass;

    //cl->staticFun((void*)cl->s1);//this is no good
    
    cl->staticFun((void*)cl); //Change it to this
    delete cl;

} 


EDIT:
I wonder if it would not be better if the staticFun function took a someclass pointer rather than a void pointer
Last edited on
Correct if I am wrong but those solution have different approaches.

The first one tries to access elements in the same object while the second case creates a new object and refers to that.

@Ingeborg I am not sure you can define a class like that (that's why your compiler complains) since at compile time your compiler does not know all parts of the class only, only at run time it does.

P.S. Finally I suggest using static_cast<example *> instead of (example *) as more safe.
Hi!

thank you for your help until now. It has given me some possibilities to think more about that problem. I need an object intern use of the data, problem. The only possibilty ive found to pass the data is to pass it with the "this"-pointer in the static function, but that forbids the compiler.

@eypros, it says, that it doesnt find the pointers, because of notdeclaring in the scope. Why it doesn't know that? Could it be, that it has something to do with the static function? The casting was not that problem. My feeling says now, that it has something to do with that, because static functions should operate without an object. It would be the same reason why a this-pointer cannot be passed in a static function...
Topic archived. No new replies allowed.