fork download
#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();
}
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
From B::genericFunction: A::specialFunction()
From B::genericFunction: A::specialFunction()