#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();
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKc3RydWN0IEE7CgpzdHJ1Y3QgQgp7CiAgICBCKCkgOiBfYShudWxscHRyKSB7fQoKICAgIHZvaWQgc2V0QShBKmEpIHsgX2EgPSBhOyB9CiAgICB2b2lkIGdlbmVyaWNGdW5jdGlvbigpOwoKcHJpdmF0ZToKICAgIEEqIF9hOwp9OwoKc3RydWN0IEEKewogICAgdm9pZCBhZGRCKEIqYik7CiAgICB2b2lkIGl0ZXJhdGUoKTsKICAgIHZvaWQgc3BlY2lhbEZ1bmN0aW9uKCk7Cgpwcml2YXRlOgogICAgc3RkOjp2ZWN0b3I8Qio+IF9iOwp9OwoKdm9pZCBCOjpnZW5lcmljRnVuY3Rpb24oKQp7CiAgICBzdGQ6OmNvdXQgPDwgIkZyb20gQjo6Z2VuZXJpY0Z1bmN0aW9uOiAiOwogICAgaWYgKF9hKQogICAgICAgIF9hLT5zcGVjaWFsRnVuY3Rpb24oKTsKfQoKdm9pZCBBOjphZGRCKEIqIGIpCnsKICAgIF9iLnB1c2hfYmFjayhiKTsKICAgIGItPnNldEEodGhpcyk7Cn0KCnZvaWQgQTo6aXRlcmF0ZSgpCnsKICAgIGZvciAoYXV0byBiIDogX2IpCiAgICAgICAgYi0+Z2VuZXJpY0Z1bmN0aW9uKCk7Cn0KCnZvaWQgQTo6c3BlY2lhbEZ1bmN0aW9uKCkKewogICAgc3RkOjpjb3V0IDw8ICJBOjpzcGVjaWFsRnVuY3Rpb24oKVxuIjsKfQoKaW50IG1haW4oKQp7CiAgICBCIGIxOwogICAgQiBiMjsKCiAgICBBIGE7CiAgICBhLmFkZEIoJmIxKTsKICAgIGEuYWRkQigmYjIpOwoKICAgIGEuaXRlcmF0ZSgpOwp9