Vector, class and pointer

Suppose that I have a class which has a vector of pointer to another class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class b
{
public:
  void genericFunction();
}
=========================
#include <vector>
 class a
{
public:
  void specialFunction();
private:
  vector<b*> vecToB;
};

How can I call specialFunction() from genericFunction()? I mean how can I have a pointer to a container of vector (class A)?
Last edited on
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
#include <iostream>
#include <vector>

struct A;

struct B
{
    B() : _a(nullptr) {}

    void setA(A*a) { _a = a; }
    void genericFunction();

private:
    A* _a;
};

struct A
{
    void addB(B*b);
    void iterate();
    void specialFunction();

private:
    std::vector<B*> _b;
};

void B::genericFunction()
{
    std::cout << "From B::genericFunction: ";
    if (_a)
        _a->specialFunction();
}

void A::addB(B* b)
{
    _b.push_back(b);
    b->setA(this);
}

void A::iterate()
{
    for (auto b : _b)
        b->genericFunction();
}

void A::specialFunction()
{
    std::cout << "A::specialFunction()\n";
}

int main()
{
    B b1;
    B b2;

    A a;
    a.addB(&b1);
    a.addB(&b2);

    a.iterate();
}


http://ideone.com/lnf1UB
Topic archived. No new replies allowed.