Need some help with prointers to member function

i need to create a database of references to specific member functions of different classes, and call all of them trough a function.

This is a code example of what i want to do:
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
#include <iostream>
#include <vector>

using namespace std;

class DrawData
{
public:
    vector <void*> p;

    void Add(void *q)
    {
        p.push_back(q);
    }

    void Draw()
    {
        for(unsigned int i=0;i<p.size();i++)
        {
            (*p[i])();
        }
    }
};

DrawData DrawData;

class a
{
public:
    a()
    {
        DrawData.Add(&func());
    }

    void func()
    {
        cout<<"\nclass a";
    }
};

class b
{
public:
    b()
    {
        DrawData.Add(&func());
    }
    void func()
    {
        cout<<"\nclass b";
    }
};

int main()
{
    a a;
    b b;
    DrawData.Draw();
}


I get those errors:
1
2
3
4
5
6
D:\C++\i\i.cpp: In member function 'void DrawData::Draw()':
D:\C++\i\i.cpp:20:18: error: 'void*' is not a pointer-to-object type
D:\C++\i\i.cpp: In constructor 'a::a()':
D:\C++\i\i.cpp:32:28: error: lvalue required as unary '&' operand
D:\C++\i\i.cpp: In constructor 'b::b()':
D:\C++\i\i.cpp:46:28: error: lvalue required as unary '&' operand
Last edited on
You are better do with virtual functions:
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <vector>

using namespace std;

class Base
{
public:
    virtual ~Base() {}
    virtual DrawFunc() = 0;
};

class DrawData
{
public:
    vector <Base*> p;

    ~DrawData()
    {
        ReleaseAll();
    }

    a* AddClassA(...)
    {
        a* aptr = new a;
        aptr->set(...);//set some members resources
        p.push_back(aptr);
        return aptr;
    }

    b* AddClassB(...)
    {
        b* bptr = new b;
        bptr->set(...);//set some members resources
        p.push_back(bptr);
        return bptr;
    }

    void DrawAll()
    {
        std::vector<Base*>::iterator iBase;
        for(iBase = p.begin();iBase != p.end(); ++iBase)
        {
            Base* ptr = *iBase;
            ptr->DrawFunc();
        }
    }

    void ReleaseAll()
    {
         std::vector<Base*>::iterator iBase;
        for(iBase = p.begin();iBase != p.end(); ++iBase)
        {
            Base* ptr = *iBase;
            delete ptr;
            ptr = 0;
        }
    }
};

class a : public Base
{
public:

    void DrawFunc()
    {
        cout<<"\nclass a specific drawing";
    }
};

class b : public Base
{
public:
   
    void DrawFunc()
    {
        cout<<"\nclass b specific drawing";
    }
};

int main()
{
    DrawData* drawer = new DrawData;
    a* aptr = drawer->AddClassA();
    b* bptr = drawer->AddClassB();

    aptr->SetPosition(...);
   aptr->SetSomethingElse();

   bptr->Something(...);
    
     drawer->DrawAll();
    
    //at end clean all
    delete drawer;
   return 0;
  
}


Might be some typos in code above!
Last edited on
ok, but i want to make it independent of the number of functions i will add and the class which they belong
You want to store pointers to a member functions in a vector? Is this even possible?
probably, i don't know

EDIT: to be more specific,I want to make a database of pointers to member functions.

I don't care if it's a vector, array, list as long as i can add/delete elements in and call the function pointed by each element.
Last edited on
Solved!
I used a base class and virtual functions:
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
#include <iostream>
#include <vector>

using namespace std;

class Drawnable
{
public:
    unsigned short prior;
    virtual void Draw()=0;
};

class DrawData
{
public:
    vector <Drawnable*> p;
    unsigned short priormax;

    DrawData()
    {
        priormax=0;
    }

    void Add(Drawnable &q)
    {
        p.push_back(&q);
        if(q.prior>priormax) priormax=q.prior;
    }

    void DrawAll()
    {
        for(unsigned short pr=0;pr<=priormax;pr++)
        {
            for(unsigned int i=0;i<p.size();i++)
            {
                if(p[i]->prior==pr) p[i]->Draw();
            }
        }
    }
};

DrawData DrawData;

class a :public Drawnable
{
public:
    a()
    {
        prior=0;
        DrawData.Add(*this);
    }

    void Draw()
    {
        cout<<"\nclass a";
    }
};

class b :public Drawnable
{
public:
    b()
    {
        prior=1;
        DrawData.Add(*this);
    }
    void Draw()
    {
        cout<<"\nclass b";
    }
};

int main()
{
    a a;
    b b;
    DrawData.DrawAll();
}
Topic archived. No new replies allowed.